当前位置:   article > 正文

鸿蒙 JS FA 调用java PA简单实例_鸿蒙 如何调用java

鸿蒙 如何调用java

核心:

一. Java PA 端

        需要继承 Ability,重写 onConnect 方法并返回 RemoteObject对象,业务逻辑在RemoteObject  onRemoteRequest方法中处理。代码如下:

  1. public class DemoService extends Ability {
  2. MyRemote myRemote = new MyRemote();
  3. //日志
  4. private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");
  5. @Override
  6. protected IRemoteObject onConnect(Intent intent) {
  7. //返回RemoteObject的对象
  8. return myRemote.asObject();
  9. }
  10. class MyRemote extends RemoteObject implements IRemoteBroker {
  11. public MyRemote() {
  12. super("descriptor");
  13. }
  14. @Override
  15. public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply,MessageOption option) throws RemoteException {
  16. return true;
  17. }
  18. @Override
  19. public IRemoteObject asObject() {
  20. return this;
  21. }
  22. }
  23. }

注意写完service类以后,需要 config.json 中注册service类,代码如下:

  1. "abilities": [
  2. {
  3. "skills": [
  4. {
  5. "entities": [
  6. "entity.system.home"
  7. ],
  8. "actions": [
  9. "action.system.home"
  10. ]
  11. }
  12. ],
  13. "visible": true,
  14. "name": "com.jiakejian.myapplication.MainAbility",
  15. "icon": "$media:icon",
  16. "description": "$string:mainability_description",
  17. "label": "$string:entry_MainAbility",
  18. "type": "page",
  19. "launchType": "standard"
  20. },
  21. {
  22. "name": "com.jiakejian.myapplication.DemoService",
  23. "icon": "$media:icon",
  24. "description": "DemoService",
  25. "type": "service"
  26. }
  27. ],

二.JS FA端

简单写了一个hml页面,添加了一个点击事件:

  1. <div class="container">
  2. <button class="title" @click="onClick">
  3. 向java FA传递数据
  4. </button>
  5. </div>

 js中需要添加 action 对象,相当于javaFA跳转中的intent ,并设为 异步方法。代码如下:

  1. onClick:async function(){
  2. //要传递的数据
  3. var data = {
  4. id:"001",
  5. name:"xiaoli"
  6. }
  7. //action对象,相当于intent
  8. var action = {};
  9. //添加service所在的包名
  10. action.bundleName = 'com.jiakejian.myapplication';
  11. //添加service的类名
  12. action.abilityName = 'com.jiakejian.myapplication.DemoService';
  13. //添加messageCode ,用于区分其他的action
  14. action.messageCode = 100;
  15. //添加数据
  16. action.data = data;
  17. //跳转的activity的类型,0代表service
  18. action.abilityType = 0;
  19. //最重要的一步:跳转
  20. var str = await FeatureAbility.callAbility(action);
  21. //下面是处理 java FA 返回的数据
  22. var result = JSON.parse(str);
  23. console.log(result.id);
  24. console.log(result.name);
  25. }

三 .Java FA端  在RemoteObject对象的 onRemoteRequest方法中 处理数据并返回数据,代码如下:

  1. @Override
  2. public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply,MessageOption option) throws RemoteException {
  3. switch (code) {
  4. case 100:
  5. String str = data.readString();
  6. HiLog.info(LABEL_LOG, "str:"+str);
  7. //添加返回数据
  8. Map<String, Object> result = new HashMap<String, Object>();
  9. result.put("id", "002");
  10. result.put("name", "xiaohuang");
  11. reply.writeString(ZSONObject.toZSONString(result));
  12. break;
  13. default:
  14. return false;
  15. }
  16. return true;
  17. }

执行结果如下:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/300571
推荐阅读
相关标签
  

闽ICP备14008679号