赞
踩
官方文档:多端协同开发指导(基于java开发)
因为多端协同它发送端这边可以启动PA/FA,但是只能停止PA,然后嘞连接操作和断开也都只能适用PA,所以我这个代码最终的展示状态只能靠看日志,且因为我只有一根数据线,所以只能看玩接收端的数据之后,再碰运气看看发送端的日志还能不能出来,如果出不来就只能再操作一遍来看日志。
然后嘞,我这个设备的情况是,Mate 10 Pro作为发送端,MatePad Pro作为接收端的时候,有一个InterfaceToken的值是可以正常拿到的,但是当MatePad Pro作为发送端,Mate 10 Pro作为接收端的时候,这个InterfaceToken的值就开始出问题了= =找了同行的同学一起测试,也没测个所以然出来。这个问题先保留在这里叭。
HUAWEI MatePad Pro 10.8英寸 2019款 HarmonyOS3.0.0.163(接收端)
HUAWEI Mate 10 Pro 全网通版 HarmonyOS2.0.0(发送端)
DevEco Studio 3.0.0.993 Release
SDK 6(使用java)
我看的《公共事件开发指导》更新于2022-10-28 18:02
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <Text ohos:id="$+id:text_register" ohos:height="match_content" ohos:width="match_content" ohos:layout_alignment="horizontal_center" ohos:text="注册流转服务" ohos:text_size="40vp" /> <Text ohos:id="$+id:text_show" ohos:height="match_content" ohos:width="match_content" ohos:layout_alignment="horizontal_center" ohos:text="列出可流转的设备" ohos:scrollable="true" ohos:text_size="40vp" /> <Text ohos:id="$+id:text_startSynergy" ohos:height="match_content" ohos:width="match_content" ohos:layout_alignment="horizontal_center" ohos:text="启动远端程序" ohos:scrollable="true" ohos:text_size="40vp" /> <Text ohos:id="$+id:text_stopSynergy" ohos:height="match_content" ohos:width="match_content" ohos:layout_alignment="horizontal_center" ohos:text="停止远端程序" ohos:scrollable="true" ohos:text_size="40vp" /> <Text ohos:id="$+id:text_connectSynergy" ohos:height="match_content" ohos:width="match_content" ohos:layout_alignment="horizontal_center" ohos:text="连接远端服务" ohos:scrollable="true" ohos:text_size="40vp" /> <Text ohos:id="$+id:text_operateSynergy" ohos:height="match_content" ohos:width="match_content" ohos:layout_alignment="horizontal_center" ohos:text="操作远端服务" ohos:scrollable="true" ohos:text_size="40vp" /> <Text ohos:id="$+id:text_disconnectSynergy" ohos:height="match_content" ohos:width="match_content" ohos:layout_alignment="horizontal_center" ohos:text="断开远端服务" ohos:scrollable="true" ohos:text_size="40vp" /> </DirectionalLayout>
长度问题只放module部分。
"module": { "package": "com.openvalley.cyj.synergy", "name": ".MyApplication", "mainAbility": "com.openvalley.cyj.synergy.MainAbility", "deviceType": [ "phone", "tablet" ], "distro": { "deliveryWithInstall": true, "moduleName": "entry", "moduleType": "entry", "installationFree": false }, "abilities": [ { "skills": [ { "entities": [ "entity.system.home" ], "actions": [ "action.system.home" ] } ], "name": "com.openvalley.cyj.synergy.MainAbility", "description": "$string:mainability_description", "icon": "$media:icon", "label": "$string:entry_MainAbility", "launchType": "standard", "orientation": "unspecified", "visible": true, "type": "page" } ], "reqPermissions": [ { "name": "ohos.permission.DISTRIBUTED_DATASYNC" }, { "name": "com.huawei.permission.ACCESS_SERVICE_DM" } ] }
package com.openvalley.cyj.synergy; import com.openvalley.cyj.synergy.slice.MainAbilitySlice; import ohos.aafwk.ability.Ability; import ohos.aafwk.ability.IAbilityContinuation; import ohos.aafwk.content.Intent; import ohos.aafwk.content.IntentParams; import ohos.bundle.IBundleManager; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; import ohos.security.SystemPermission; public class MainAbility extends Ability implements IAbilityContinuation { private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00001, MainAbility.class.getSimpleName()); @Override public void onStart(Intent intent) { super.onStart(intent); super.setMainRoute(MainAbilitySlice.class.getName()); verifyAndRequestSelfPermission(); } /** * 在此处验证和请求用户权限 */ private void verifyAndRequestSelfPermission() { try{ //返回值0有,-1没有 if(verifyCallingOrSelfPermission(SystemPermission.DISTRIBUTED_DATASYNC) == IBundleManager.PERMISSION_GRANTED){ // if(verifySelfPermission(SystemPermission.LOCATION) == IBundleManager.PERMISSION_GRANTED){ //如果有权限 }else { //如果没授权就看这个权限能否动态授权 if(canRequestPermission(SystemPermission.DISTRIBUTED_DATASYNC)){ //如果可以弹窗授权 requestPermissionsFromUser(new String[] {SystemPermission.DISTRIBUTED_DATASYNC}, 0x10001); }else { //如果不能动态授权 return ; } } }catch (Exception e){ e.printStackTrace(); } } /** * 授权的回调 * @param requestCode * @param permissions * @param grantResults */ @Override public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults); if(requestCode == 0x10001){ if(grantResults[0] == IBundleManager.PERMISSION_GRANTED){ //如果授权成功(0) HiLog.info(LABEL_LOG, permissions[0] + "权限授权成功。"); }else { //如果没有授权成功(-1) return ; } } } /** * 准备开始迁移的回调 * @return */ @Override public boolean onStartContinuation() { //返回值为true才发起迁移 HiLog.info(LABEL_LOG, "MainAbility onStartContinuation"); return true; } /** * 当保存数据(要迁移的数据)的回调 * @param intentParams * @return */ @Override public boolean onSaveData(IntentParams intentParams) { HiLog.info(LABEL_LOG, "MainAbility onSaveData"); return true; } /** * 当恢复数据的回调(应该是被迁移的设备调用 * @param intentParams * @return */ @Override public boolean onRestoreData(IntentParams intentParams) { //返回为true代表恢复数据成功 HiLog.info(LABEL_LOG, "MainAbility onRestoreData"); return true; } /** * 迁移完成的回调 * @param i */ @Override public void onCompleteContinuation(int i) { HiLog.info(LABEL_LOG, " MainAbility onCompleteContinuation"); } /** * 迁移过程中失败的回调 */ @Override public void onRemoteTerminated() { IAbilityContinuation.super.onRemoteTerminated(); HiLog.info(LABEL_LOG, "MainAbility onRemoteTerminated"); } /** * 回迁允许的情况下,远端中断 * @param errorCode */ @Override public void onFailedContinuation(int errorCode) { IAbilityContinuation.super.onFailedContinuation(errorCode); HiLog.info(LABEL_LOG, "MainAbility onFailedContinuation"); } }
package com.openvalley.cyj.synergy.slice; import com.openvalley.cyj.synergy.ResourceTable; import com.openvalley.cyj.synergy.pojo.MyRemoteProxy; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.ability.IAbilityConnection; import ohos.aafwk.ability.IAbilityContinuation; import ohos.aafwk.ability.continuation.*; import ohos.aafwk.content.Intent; import ohos.aafwk.content.IntentParams; import ohos.aafwk.content.Operation; import ohos.agp.components.Text; import ohos.bundle.ElementName; import ohos.distributedschedule.interwork.DeviceManager; import ohos.distributedschedule.interwork.IInitCallback; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; import ohos.rpc.IRemoteObject; import ohos.rpc.RemoteException; public class MainAbilitySlice extends AbilitySlice implements IAbilityContinuation { private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00001, MainAbilitySlice.class.getSimpleName()); Text text_register = null; Text text_show = null; Text text_startSynergy = null; Text text_stopSynergy = null; Text text_connectSynergy = null; Text text_operateSynergy = null; Text text_disconnectSynergy = null; IContinuationRegisterManager continuationRegisterManager = null; IContinuationDeviceCallback continuationDeviceCallback = null; RequestCallback requestCallback = null; IInitCallback initCallback = null; IAbilityConnection abilityConnection = null; String jsonParams = null; int abilityToken = 0; String deviceId = null; MyRemoteProxy remoteProxy = null; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); initContinuationRegisterManager(); initComponent(); } /** * 初始化注册管理类 */ private void initContinuationRegisterManager(){ continuationRegisterManager = getContinuationRegisterManager(); //设备的回调(是在列出了设备列表后,选择某个设备进行流转连接成功或者断开时调用) continuationDeviceCallback = new IContinuationDeviceCallback() { @Override public void onConnected(ContinuationDeviceInfo deviceInfo) { try { HiLog.info(LABEL_LOG, "onConnected()"); deviceId = deviceInfo.getDeviceId(); HiLog.info(LABEL_LOG, "onConnected() 连接成功。 deviceId = " + deviceId); //多端协同需要初始化分布式环境 DeviceManager.initDistributedEnvironment(deviceId, initCallback); continuationRegisterManager.updateConnectStatus(abilityToken, deviceId, DeviceConnectState.CONNECTED.getState(), requestCallback); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onDeviceConnectDone(String s, String s1) { HiLog.info(LABEL_LOG, "onDeviceConnectDone()"); } @Override public void onDisconnected(String deviceId) { HiLog.info(LABEL_LOG, "onDisconnected()"); } @Override public void onDeviceDisconnectDone(String s) { HiLog.info(LABEL_LOG, "onDeviceDisconnectDone()"); } }; //请求的回调(注册到服务中心成功) requestCallback = new RequestCallback() { @Override public void onResult(int i) { if(i > 0){ //为了保证更新数据时这个值还在,所以在if里写 abilityToken = i; HiLog.info(LABEL_LOG, "onResult() 注册成功。 abilityToken = " + abilityToken); }else { HiLog.info(LABEL_LOG, "onResult() 其他操作。 abilityToken = " + i); } } }; //初始化的回调 initCallback = new IInitCallback() { @Override public void onInitSuccess(String s) { HiLog.info(LABEL_LOG, "onInitSuccess() 初始化分布式环境成功 deviceId = " + s); } @Override public void onInitFailure(String s, int i) { HiLog.info(LABEL_LOG, "onInitSuccess() 初始化分布式环境成功 deviceId = " + s + " i = " + i); } }; //服务连接的回调 abilityConnection = new IAbilityConnection() { @Override public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int i) { HiLog.info(LABEL_LOG, "onAbilityConnectDone() 服务连接成功。"); //初始化本地代理 remoteProxy = new MyRemoteProxy(iRemoteObject); } @Override public void onAbilityDisconnectDone(ElementName elementName, int i) { HiLog.info(LABEL_LOG, "onAbilityDisconnectDone() 服务断开连接。"); } }; } /** * 初始化组件 */ private void initComponent(){ text_register = (Text)findComponentById(ResourceTable.Id_text_register); text_register.setClickedListener(listen -> register()); text_show = (Text)findComponentById(ResourceTable.Id_text_show); text_show.setClickedListener(listen -> show()); text_startSynergy = (Text)findComponentById(ResourceTable.Id_text_startSynergy); text_startSynergy.setClickedListener(listen -> startSynergy()); text_stopSynergy = (Text)findComponentById(ResourceTable.Id_text_stopSynergy); text_stopSynergy.setClickedListener(listen -> stopSynergy()); text_connectSynergy = (Text)findComponentById(ResourceTable.Id_text_connectSynergy); text_connectSynergy.setClickedListener(listen -> connectSynergy()); text_operateSynergy = (Text)findComponentById(ResourceTable.Id_text_operateSynergy); text_operateSynergy.setClickedListener(listen -> operateSynergy()); text_disconnectSynergy = (Text)findComponentById(ResourceTable.Id_text_disconnectSynergy); text_disconnectSynergy.setClickedListener(listen -> disconnectSynergy()); } /** * 点击按钮进行流转服务注册 */ private void register(){ if(continuationRegisterManager != null){ HiLog.info(LABEL_LOG, "register() 开始注册。"); continuationRegisterManager.register(getBundleName(), getExtraParams(), continuationDeviceCallback, requestCallback); } } /** * 展示可流转的设备 */ private void show(){ if(continuationRegisterManager != null && abilityToken > 0){ continuationRegisterManager.showDeviceList(abilityToken, getExtraParams(), requestCallback); } } /** * 启动远端程序(FA/PA) */ private void startSynergy(){ Intent startIntent = new Intent(); Operation operation = new Intent.OperationBuilder() .withDeviceId(deviceId) .withBundleName("com.chenyijie") // .withAbilityName("com.chenyijie.MainAbility") .withAbilityName("com.chenyijie.ServiceAbility") //开启分布式任务标记 .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE) .build(); startIntent.setOperation(operation); startAbility(startIntent); } /** * 停止远端程序(PA) */ private void stopSynergy(){ //无法停止页面 Intent stopIntent = new Intent(); Operation operation = new Intent.OperationBuilder() .withDeviceId(deviceId) .withBundleName("com.chenyijie") .withAbilityName("com.chenyijie.ServiceAbility") //开启分布式任务标记 .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE) .build(); stopIntent.setOperation(operation); stopAbility(stopIntent); } /** * 连接远端服务(PA) */ private void connectSynergy(){ Intent connectIntent = new Intent(); Operation operation = new Intent.OperationBuilder() .withDeviceId(deviceId) .withBundleName("com.chenyijie") .withAbilityName("com.chenyijie.ServiceAbility") //开启分布式任务标记 .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE) .build(); connectIntent.setOperation(operation); boolean connectAbility = connectAbility(connectIntent, abilityConnection); HiLog.info(LABEL_LOG, "connectSynergy() connectAbility = " + connectAbility); } /** * 操作远端服务(PA) */ private void operateSynergy(){ try { int addResult = remoteProxy.add(1, 2); HiLog.info(LABEL_LOG, "operateSynergy() addResult = " + addResult); } catch (RemoteException e) { e.printStackTrace(); } } /** * 断开远端服务(PA) */ private void disconnectSynergy(){ disconnectAbility(abilityConnection); } /** * 获得注册时 额外的一些参数 * @return */ private ExtraParams getExtraParams(){ ExtraParams extraParams = new ExtraParams(); String[] devType = {ExtraParams.DEVICETYPE_SMART_PHONE, ExtraParams.DEVICETYPE_SMART_PAD}; //显示的列表包含那些设备类型 extraParams.setDevType(devType); //添加描述(没出来) extraParams.setDescription("这是一个描述。"); //如果不写,就会默认以注册时的BundleName作为targetBundleName extraParams.setTargetBundleName(getBundleName()); //设备过滤 jsonParams = "\"{" + "\"filter\":{" + "\"commonFilter\":{" + "\"system\":{" + //目标设备对应的HarmonyOS版本 要≥ "\"harmonyVersion\":\"2.0.0\"" + "}," + // "}" + //1为同账号,1|256为不同账号 "\"groupType\":\"1|256\"," + //0x00000004需要在同一局域网下,0x00030004不需要在同一局域网下 "\"curComType\": 0x00030004," + //为空就不做版本兼容性检查,要做版本兼容性检查时,需要传入目标应用包名。 "\"faFilter\":\"{" + "\"targetBundleName\":\"com.openvalley.cyj.migration\"" + "}\"" + "}" + "}," + "\"transferScene\":0," + "\"remoteAuthenticationDescription\": \"拉起HiVision扫描弹框描述\"" + "}\""; // HiLog.info(LABEL_LOG, jsonParams); extraParams.setJsonParams(jsonParams); return extraParams; } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } @Override protected void onStop() { if(continuationRegisterManager != null && abilityToken > 0){ continuationRegisterManager.unregister(abilityToken, requestCallback); } //断开流转任务管理服务连接 continuationRegisterManager.disconnect(); } /** * 准备开始迁移的回调 * @return */ @Override public boolean onStartContinuation() { //返回值为true才发起迁移 HiLog.info(LABEL_LOG, "MainAbilitySlice onStartContinuation"); return true; } /** * 当保存数据(要迁移的数据)的回调 * @param intentParams * @return */ @Override public boolean onSaveData(IntentParams intentParams) { HiLog.info(LABEL_LOG, "MainAbilitySlice onSaveData"); return true; } /** * 当恢复数据的回调(应该是被迁移的设备调用 * @param intentParams * @return */ @Override public boolean onRestoreData(IntentParams intentParams) { //返回为true代表恢复数据成功 HiLog.info(LABEL_LOG, "MainAbilitySlice onRestoreData"); return true; } /** * 迁移完成的回调 * @param i */ @Override public void onCompleteContinuation(int i) { HiLog.info(LABEL_LOG, "MainAbilitySlice onCompleteContinuation"); } /** * 迁移过程中失败的回调 */ @Override public void onRemoteTerminated() { HiLog.info(LABEL_LOG, "MainAbilitySlice onRemoteTerminated"); } /** * 回迁允许的情况下,远端中断 * @param errorCode */ @Override public void onFailedContinuation(int errorCode) { IAbilityContinuation.super.onFailedContinuation(errorCode); HiLog.info(LABEL_LOG, "MainAbilitySlice onFailedContinuation"); } }
package com.openvalley.cyj.synergy.pojo; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; import ohos.rpc.*; /** * 存根,远程通讯时本地的代理类 */ public class MyRemoteProxy implements IRemoteBroker { private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00001, MyRemoteProxy.class.getSimpleName()); public static final int DATACODE = 0x001; //请求的token public static final String DATAINTERFACETOKEN = "null"; //响应的token public static final String REPLYINTERFACETOKEN = "404"; private IRemoteObject remoteObject = null; public MyRemoteProxy(IRemoteObject iRemoteObject){ this.remoteObject = iRemoteObject; } @Override public IRemoteObject asObject() { return null; } /** * 此处定义的方法,此方法将来由远程调用 * @param a * @param b * @return * @throws RemoteException */ public int add(int a, int b) throws RemoteException { MessageParcel data = MessageParcel.obtain(); MessageParcel reply = MessageParcel.obtain(); MessageOption messageOption = new MessageOption(MessageOption.TF_SYNC); int result = 0; try{ //往请求中写入数据 data.writeInterfaceToken(DATAINTERFACETOKEN); data.writeInt(a); data.writeInt(b); // 请求码 请求 响应 可选性 remoteObject.sendRequest(DATACODE, data, reply, messageOption); //从响应中获得数据 String interfaceToken = reply.readInterfaceToken(); if(REPLYINTERFACETOKEN.equals(interfaceToken)){ //如果token匹配 result = reply.readInt(); } }finally { data.reclaim(); reply.reclaim(); } return result; } }
长度问题只放module部分。
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <Text ohos:id="$+id:text_hellWorld" ohos:height="match_content" ohos:width="match_content" ohos:layout_alignment="horizontal_center" ohos:text="$string:mainability_HelloWorld" ohos:text_size="40vp" /> </DirectionalLayout>
"module": { "package": "com.chenyijie", "name": ".MyApplication", "mainAbility": "com.chenyijie.MainAbility", "deviceType": [ "phone", "tablet" ], "distro": { "deliveryWithInstall": true, "moduleName": "entry", "moduleType": "entry", "installationFree": false }, "abilities": [ { "skills": [ { "entities": [ "entity.system.home" ], "actions": [ "action.system.home" ] } ], "name": "com.chenyijie.MainAbility", "description": "$string:mainability_description", "icon": "$media:icon", "label": "$string:entry_MainAbility", "launchType": "standard", "orientation": "unspecified", "visible": true, "type": "page" }, { "name": "com.chenyijie.ServiceAbility", "description": "$string:serviceability_description", "type": "service", "backgroundModes": [], "visible": true, "icon": "$media:icon" } ], "reqPermissions": [ { "name": "ohos.permission.DISTRIBUTED_DATASYNC" }, { "name": "com.huawei.permission.ACCESS_SERVICE_DM" } ] }
package com.chenyijie; import com.chenyijie.slice.MainAbilitySlice; import ohos.aafwk.ability.Ability; import ohos.aafwk.content.Intent; import ohos.bundle.IBundleManager; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; import ohos.security.SystemPermission; public class MainAbility extends Ability { private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00001, MainAbility.class.getSimpleName()); @Override public void onStart(Intent intent) { super.onStart(intent); super.setMainRoute(MainAbilitySlice.class.getName()); verifyAndRequestSelfPermission(); } /** * 在此处验证和请求用户权限 */ private void verifyAndRequestSelfPermission() { try{ //返回值0有,-1没有 if(verifyCallingOrSelfPermission(SystemPermission.DISTRIBUTED_DATASYNC) == IBundleManager.PERMISSION_GRANTED){ // if(verifySelfPermission(SystemPermission.LOCATION) == IBundleManager.PERMISSION_GRANTED){ //如果有权限 }else { //如果没授权就看这个权限能否动态授权 if(canRequestPermission(SystemPermission.DISTRIBUTED_DATASYNC)){ //如果可以弹窗授权 requestPermissionsFromUser(new String[] {SystemPermission.DISTRIBUTED_DATASYNC}, 0x10001); }else { //如果不能动态授权 return ; } } }catch (Exception e){ e.printStackTrace(); } } /** * 授权的回调 * @param requestCode * @param permissions * @param grantResults */ @Override public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults); if(requestCode == 0x10001){ if(grantResults[0] == IBundleManager.PERMISSION_GRANTED){ //如果授权成功(0) HiLog.info(LABEL_LOG, permissions[0] + "权限授权成功。"); }else { //如果没有授权成功(-1) return ; } } } }
package com.chenyijie.slice; import com.chenyijie.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Text; public class MainAbilitySlice extends AbilitySlice { private Text text_hellWorld = null; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }
package com.chenyijie; import com.chenyijie.pojo.MyRemoteObject; import ohos.aafwk.ability.Ability; import ohos.aafwk.content.Intent; import ohos.rpc.IRemoteObject; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; public class ServiceAbility extends Ability { private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00001, ServiceAbility.class.getSimpleName()); MyRemoteObject remoteObject = null; @Override public void onStart(Intent intent) { HiLog.info(LABEL_LOG, "ServiceAbility::onStart"); super.onStart(intent); remoteObject = new MyRemoteObject(); } @Override public void onBackground() { super.onBackground(); HiLog.info(LABEL_LOG, "ServiceAbility::onBackground"); } @Override public void onStop() { super.onStop(); HiLog.info(LABEL_LOG, "ServiceAbility::onStop"); } @Override public void onCommand(Intent intent, boolean restart, int startId) { HiLog.info(LABEL_LOG, "ServiceAbility::onCommand"); } @Override public IRemoteObject onConnect(Intent intent) { super.onConnect(intent); HiLog.info(LABEL_LOG, "ServiceAbility::onConnect"); return remoteObject.asObject(); } @Override public void onDisconnect(Intent intent) { HiLog.info(LABEL_LOG, "ServiceAbility::onDisconnect"); } }
package com.chenyijie.pojo; import com.chenyijie.ServiceAbility; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; import ohos.rpc.*; /** * 骨架 远程代理对象类 */ public class MyRemoteObject extends RemoteObject implements IRemoteBroker { private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00001, MyRemoteObject.class.getSimpleName()); public static final int DATACODE = 0x001; //请求的token public static final String DATAINTERFACETOKEN = "null"; //响应的token public static final String REPLYINTERFACETOKEN = "404"; public MyRemoteObject(){ super("description."); } @Override public IRemoteObject asObject() { return this; } /** * 这个方法就是远程调用时的方法(响应 答复) * @param code * @param data * @param reply * @param option * @return * @throws RemoteException */ @Override public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException { int result = 0; if(code == DATACODE){ //如果请求码匹配 HiLog.info(LABEL_LOG, "请求码匹配。"); String interfaceToken = data.readInterfaceToken(); if(DATAINTERFACETOKEN.equals(interfaceToken)){ //如果token匹配 //拿请求中的数据 int a = data.readInt(); int b = data.readInt(); HiLog.info(LABEL_LOG, "a = " + a); HiLog.info(LABEL_LOG, "b = " + b); result = a + b; HiLog.info(LABEL_LOG, "result = " + result); //给响应加数据 reply.writeInterfaceToken(REPLYINTERFACETOKEN); reply.writeInt(result); return true; } } return false; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。