当前位置:网站首页>[harmonyos] [arkui] how can Hongmeng ETS call pa
[harmonyos] [arkui] how can Hongmeng ETS call pa
2022-06-25 20:01:00 【Huawei Developer Forum】
【 Problem description 】

The document describes js call pa Case study , No, ets call pa Use cases ? The next step is ets How to call pa
Reference document link
【 Solution 】
First step : newly build pa Of Ability( Currently named MyServiceAbility)
Links to resources
The code is as follows
// ohos Related interface package public class ComputeInternalAbility extends AceInternalAbility { private static final String BUNDLE_NAME = "com.harmony.alliance.myets"; private static final String ABILITY_NAME = "com.harmony.alliance.myets.ComputeInternalAbility"; private static final int SUCCESS = 0; private static final int ERROR = 1; private static final int PLUS = 1001;// Define log labels private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0, "MY_TAG"); private static ComputeInternalAbility instance; private AbilityContext abilityContext; // If more than one Ability The current instance needs to be registered InternalAbility example , The constructor needs to be changed , Set your own bundleName and abilityName public ComputeInternalAbility() { super(BUNDLE_NAME, ABILITY_NAME); } /** * Internal ability Registered interface . */ public static void register(AbilityContext abilityContext) { instance = new ComputeInternalAbility(); instance.onRegister(abilityContext); } /** * Internal ability Logout interface . */ public static void unregister() { instance.onUnregister(); } public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) { switch (code) { case PLUS: { System.err.println("app log pa obtain ets data "+data.readString()); Map<String, Object> result = new HashMap<String, Object>(); result.put("code", SUCCESS); result.put("abilityResult", "Pa data"); MessageParcel responseData = MessageParcel.obtain(); responseData.writeString(ZSONObject.toZSONString(result)); IRemoteObject remoteReply = reply.readRemoteObject(); try { remoteReply.sendRequest(0, responseData, MessageParcel.obtain(), new MessageOption()); } catch (RemoteException exception) { return false; } finally { responseData.reclaim(); } break; } default: { Map<String, Object> result = new HashMap<String, Object>(); result.put("abilityError", ERROR); reply.writeString(ZSONObject.toZSONString(result)); return false; } } return true; } private void onRegister(AbilityContext abilityContext) { this.abilityContext = abilityContext; this.setInternalAbilityHandler((code, data, reply, option) -> { return this.onRemoteRequest(code, data, reply, option); }); } private void onUnregister() { abilityContext = null; this.setInternalAbilityHandler(null); }}The second step see config.json whether abilities Whether to Pa The configuration file

The third step Writing MainAbility Of onStart() and onStop() Code The code is as follows
package com.harmony.alliance.myets; import ohos.ace.ability.AceAbility;import ohos.aafwk.content.Intent;import ohos.security.SystemPermission; public class MainAbility extends AceAbility { @Override public void onStart(Intent intent) { ComputeInternalAbility.register(this); super.onStart(intent); }@Override public void onStop() { ComputeInternalAbility.unregister(); super.onStop(); SampleJSInterface.getInstance().deregister(this); }}Step four Writing ets file
@[email protected] MyService { build() {Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Text('ets call pa') .fontSize(50) .onClick(function () { FeatureAbility.callAbility({ bundleName: 'com.harmony.alliance.myets', abilityName: 'com.harmony.alliance.myets.ComputeInternalAbility', messageCode: 1001, abilityType: 1, data: { firstNum: 1, secondNum: 2 }, syncOption: 0 }).then((data) => { console.log('pa Pass to ets The data of ' + data); }) }) .fontWeight(FontWeight.Bold) } .width('100%') .height('100%') }} Running effect : 
More highlights , Please see the official Huawei Developer Forum →https://developer.huawei.com/consumer/cn/forum/home?ha_source=sanfang
边栏推荐
- PHP little knowledge record
- Laravel validation rule followed Role of auth:: id()
- Redis cache preheating & avalanche & breakdown & penetration
- Thymleaf template configuration analysis
- Pta--7-20 exchange minimum and maximum (15 points)
- 2.2 step tariff
- Server journey from scratch - Yu Zhongxian integrated version (IP access server, LNMP compilation and installation, Lua environment and socket expansion)
- Automatic fitting when the applet reaches the top
- <C>. Rolling phase division
- Is it safe to open a new bond securities account
猜你喜欢

wooyun-2014-065513

Uni app through uni Navigateto failed to pass parameter (pass object)

New features of redis 6.0: take you 100% to master the multithreading model

2020-12-09 laravel . Env file loading mechanism process

Web components - Basics

Vulnhub range - darkhole 1

<C>. Figure guessing game

What are Baidu collection skills? 2022 Baidu article collection skills

What should I pay attention to in GoogleSEO content station optimization?

Now meditation: crash service and performance service help improve application quality
随机推荐
Redis is a loser. If you don't understand the usage specification, you will spoil it
Redis cache preheating & avalanche & breakdown & penetration
The native JS mobile phone sends SMS cases. After clicking the button, the mobile phone number verification code is sent. The button needs to be disabled and re enabled after 60 seconds
Is it safe to open an account with flush?
Read multiple associations from a field using delimiters in laravel
Please do not call Page constructor in files
Hdoj topic 2005 day
Print 1 cute every 100 milliseconds ~ with a running lantern effect
Two types of attribute injection methods
DataX script task development record
Applet canvas generate sharing Poster
JS asynchronism (III. usage of generator and async/await)
Now meditation: crash service and performance service help improve application quality
PostgreSQL user role permissions
Arduino : No such file or directory
Analyse du code source du processus d'acquisition et de connexion hikaricp II
6、 Configuration resolution of hikariconfig
Web container basic configuration
2020-12-09 laravel . Env file loading mechanism process
Case: count the most characters and times