鸿蒙尝试(二)

环境和其它配置请看(一)

布局部分 想到哪里写到哪里了。。。

1,Text 换行显示

    ohos:multiple_lines="true"

2,鸿蒙日志

HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00201, "TAG");
HiLog.warn(label, "测试", url);

微信截图_20210719113501.png

3,页面跳转 启动和传值


image.png
   Intent intent1 = new Intent();

            Operation operation = new Intent.OperationBuilder()
                    .withAction("action.pay")
                    .withDeviceId("1")
                    .withBundleName("xxx")
                    .withAbilityName("yyy")
                    .build();
            intent1.setOperation(operation);
            intent1.setParam("value", "10");
            present(new MainAbilitySlice(), intent1);

也可以启动界面
ohos.samples.backgrounddownload 是包名。。。。。。

 Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder().withDeviceId("")
            .withBundleName("ohos.samples.backgrounddownload")
            .withAbilityName("ohos.samples.backgrounddownload.AnotherAbility")
            .build();
        intent.setOperation(operation);
        startAbility(intent);

4,android 中handler 的使用,在鸿蒙里面EventHandler

private class EventHandler1 extends EventHandler {

        private EventHandler1(EventRunner runner) {
            super(runner);
        }

        @Override
        public void processEvent(InnerEvent event) {
            switch (event.eventId) {
                case 100:
                    /*获取参数*/
                    Object object = event.object;
                    Object param = event.param;
                    getUITaskDispatcher().asyncDispatch(() ->{

                    });
                    break;
            }
        }
    }
private void initHandler() {
        EventHandler1 handler1 = new EventHandler1(EventRunner.create("EventHandler1"));
        /*普通发送*/
        handler1.sendEvent(100);
        InnerEvent normalInnerEvent = InnerEvent.get(EVENT_MESSAGE_NORMAL, 10, "hello");
        /*普通 选择参数发送*/
        handler1.sendEvent(normalInnerEvent, EventHandler.Priority.IMMEDIATE);
        /*延时发送*/
        handler1.sendEvent(normalInnerEvent, 1000, EventHandler.Priority.IMMEDIATE);

        Runnable task1 = () -> {
            getUITaskDispatcher().asyncDispatch(() -> {
                resultText.setText(stringBuffer.toString());

            });
        };
        /*post 一个runable*/
        handler1.postTask(task1, EventHandler.Priority.IMMEDIATE);
        /*post 延时 发送一个runable*/
        handler1.postTask(task1, 1000, EventHandler.Priority.IMMEDIATE);

    }

5,异步处理。
三种情况
1,globalTaskDispatcher.syncDispatch(() -> stringBuffer.append("Sync task1 run" + System.lineSeparator()));最常见
2,中有添加一个多少时间后处理。

globalTaskDispatcher.delayDispatch(() -> {
            stringBuffer.append("DelayDispatch task1 run" + System.lineSeparator());
            final long actualDelayMs = System.currentTimeMillis() - callTime;
            stringBuffer.append("ActualDelayTime >= delayTime : " + (actualDelayMs >= DELAY_TIME));
            handler.postSyncTask(() -> resultText.setText(stringBuffer.toString()));
        }, DELAY_TIME);

3,

 TaskDispatcher dispatcher = createParallelTaskDispatcher("", TaskPriority.DEFAULT);
 Group group = dispatcher.createDispatchGroup();

Group 的使用。这个可以有多个异步条件进行。

6,线程池的使用
ThreadPoolExecutor

创建

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        throw new RuntimeException("Stub!");
    }

传入runable就可以了

private static ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_COUNT, THREAD_COUNT, KEEP_ALIVE,
        TimeUnit.SECONDS, new ArrayBlockingQueue<>(WORK_QUEUE_SIZE), new CommonThreadFactory());
 
    /**
     * Submit task to execute
     *
     * @param task runnable task
     */
    public static void submit(Runnable task) {
        executor.submit(task);
    }

7,网络请求
官方请求方式

URL url = new URL(inputText.getText());
                URLConnection urlConnection = url.openConnection();
                if (urlConnection instanceof HttpURLConnection) {
                    HttpURLConnection connection = (HttpURLConnection)urlConnection;
                    connection.connect();
                    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
                        ImageSource imageSource = ImageSource.create(connection.getInputStream(), srcOpts);
                        PixelMap pixelMap = imageSource.createPixelmap(null);
                        getUITaskDispatcher().syncDispatch(() -> image.setPixelMap(pixelMap));
                    }
                    connection.disconnect();
                }

这个需要线程里面进行。

这边没有真机,测试机里面是华为内部网络。
鸿蒙里面多了一个HttpResponseCache。
// 初始化时设置缓存目录dir及最大缓存空间
HttpResponseCache.install(dir, 10 * 1024 * 1024);

// 访问URL

// 为确保缓存保存到文件系统可以执行flush操作
HttpResponseCache.getInstalled().flush();

// 结束时关闭缓存
HttpResponseCache.getInstalled().close();

8,DatagramSocket 这个使用
通过Socket绑定来进行数据传输
发送

 private void netRequest(Component component) {
        ThreadPoolUtil.submit(() -> {
            NetManager netManager = NetManager.getInstance(null);
            if (!netManager.hasDefaultNet()) {
                return;
            }
            try (DatagramSocket socket = new DatagramSocket()) {
                NetHandle netHandle = netManager.getDefaultNet();
                InetAddress address = netHandle.getByName(inputText.getText());
                netHandle.bindSocket(socket);
                byte[] buffer = "I'm from Client".getBytes();
                DatagramPacket request = new DatagramPacket(buffer, buffer.length, address, PORT);
                socket.send(request);
            } catch (IOException e) {
                HiLog.error(LABEL_LOG, "%{public}s", "netRequest IOException");
            }
        });
    }

接受。是服务器端

private void startServer(Component component) {
        ThreadPoolUtil.submit(() -> {
            try (DatagramSocket socket = new DatagramSocket(PORT)) {
                DatagramPacket packet = new DatagramPacket(new byte[255], 255);
                while (true) {
                    socket.receive(packet);
                    getUITaskDispatcher().syncDispatch(() -> {
                        outText.setText(
                            "Receive a message from :" + packet.getAddress().getHostAddress() + System.lineSeparator()
                                + " on port " + packet.getPort() + System.lineSeparator() + "message :" + new String(
                                packet.getData()).substring(0, packet.getLength()));
                    });
                    packet.setLength(255);
                    Thread.sleep(1000);
                }
            } catch (IOException | InterruptedException e) {
                HiLog.error(LABEL_LOG, "%{public}s", "StartServer  IOException | InterruptedException");
            }
        });
    }

9,鸿蒙里面的服务。这个和android 就不一样了。。

  • 基于 Service 模板的 Ability(以下简称 "Service")主要用于后台运行任务(如执行音乐播放、文件下载等),但不提供用户交互界面。

    Service 可由其他应用或 Ability 启动,即使用户切换到其他应用,Service 仍将在后台继续运行。
    例如下载。
    方式1.界面启动了,后台进行下载,不耽误你做任何事情。app 挂了,或界面关闭了,下载停止。
    这种是AbilityConnection 连接,还不适于服务的一种吧。

public class DownloadServiceConnection implements IAbilityConnection {
 @Override
    public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int resultCode) {
        LogUtil.info(TAG, "on Ability Connect Done");
        sendHandlerMessage("service connect done");
        downloadServiceProxy = new DownloadServiceProxy(iRemoteObject);
    }
}
他会有两个方法,连接成功和连接失败两个方法。
下载方法在代理里面进行

private class DownloadServiceProxy implements IRemoteBroker {
下载方法在这个里面进行

}

通过handle 进行消息发送,在通过自己写接口进行回调。

鸿蒙里面有个下载接口。 还可以多下载进行。

方式2.
主app 切换其他app 不受影响。
启动

 private void startLocalService(String bundleName, String serviceName) {
        Intent localServiceIntent = getLocalServiceIntent(bundleName, serviceName);
        startAbility(localServiceIntent);
    }

    private void startRemoteService(String bundleName, String serviceName) {
        Intent remoteServiceIntent = getRemoteServiceIntent(bundleName, serviceName);
        startAbility(remoteServiceIntent);
    }

    private Intent getLocalServiceIntent(String bundleName, String serviceName) {
        Operation operation = new Intent.OperationBuilder().withDeviceId("")
            .withBundleName(bundleName)
            .withAbilityName(serviceName)
            .build();
        Intent intent = new Intent();
        intent.setOperation(operation);
        return intent;
    }

/**
 * LocalServiceAbility
 */
public class LocalServiceAbility extends Ability {
    private static final String TAG = MainAbilitySlice.class.getSimpleName();
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, TAG);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        HiLog.info(LABEL_LOG, "%{public}s",  "onStart");
        showTips(this, "LocalService onStart");
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
        super.onCommand(intent, restart, startId);
        HiLog.info(LABEL_LOG, "%{public}s",  "onCommand");
        showTips(this, "LocalService onCommand");
    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        showTips(LocalServiceAbility.this, "LocalService onConnect ");
        return new CurrentRemoteObject();
    }

    @Override
    public void onDisconnect(Intent intent) {
        super.onDisconnect(intent);
        showTips(LocalServiceAbility.this, "LocalService onDisconnect ");
    }

    @Override
    public void onStop() {
        super.onStop();
        showTips(this, "LocalService onStop");
    }

    private class CurrentRemoteObject extends RemoteObject {
        private CurrentRemoteObject() {
            super("CurrentRemoteObject");
        }

        @Override
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
            HiLog.info(LABEL_LOG, "%{public}s",  "onRemoteRequest ");
            return true;
        }
    }

    private void showTips(Context context, String msg) {
        new ToastDialog(context).setText(msg).show();
    }
}

获取

   private IAbilityConnection connection = new IAbilityConnection() {
        @Override
        public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int resultCode) {
            HiLog.info(LABEL_LOG, "%{public}s", "onAbilityConnectDone resultCode : " + resultCode);
            eventHandler.sendEvent(EVENT_ABILITY_CONNECT_DONE);
            RemoteAgentProxy remoteAgentProxy = new RemoteAgentProxy(iRemoteObject);
            try {
                remoteAgentProxy.setRemoteObject("This param from client");
            } catch (RemoteException e) {
                HiLog.error(LABEL_LOG, "%{public}s", "onAbilityConnectDone RemoteException");
            }
        }

        @Override
        public void onAbilityDisconnectDone(ElementName elementName, int resultCode) {
            HiLog.info(LABEL_LOG, "%{public}s", "onAbilityDisconnectDone resultCode : " + resultCode);
            eventHandler.sendEvent(EVENT_ABILITY_DISCONNECT_DONE);
        }
    };

eventHandler这里显得就很重要了。

server需要配置cofig中配置

"abilities": [         
               {    
                   "name": ".ServiceAbility",
                   "type": "service",
                   "visible": true
                   ...
               }
           ]
  1. toast 的使用
    new ToastDialog(context).setText(msg).show();

11,也需要 权限申请

private void requestPermission() {
        if (verifySelfPermission(SystemPermission.DISTRIBUTED_DATASYNC) != IBundleManager.PERMISSION_GRANTED) {
            requestPermissionsFromUser(new String[] {SystemPermission.DISTRIBUTED_DATASYNC}, 0);
        }
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        if (permissions == null || permissions.length == 0 || grantResults == null || grantResults.length == 0) {
            return;
        }
        if (requestCode == 0) {
            if (grantResults[0] == IBundleManager.PERMISSION_DENIED) {
                terminateAbility();
            }
        }
    }

几种现象,demo有时候连接不到虚拟机,有时候点击没有反应。身边没有真测试机用。需要重新连接。需要模拟器,还得实名认证。当然打包也需要在华为后台配置和下载签名。。。这个就麻烦了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270