添加容错处理,service增加是否连接标志

Signed-off-by: guojin26 <guojin26@huawei.com>
This commit is contained in:
guojin26 2021-07-05 19:28:51 +08:00
parent 0076df13d0
commit 4fbb9046c1
2 changed files with 17 additions and 2 deletions

View File

@ -40,10 +40,13 @@ public class RemoteAbility extends Ability {
RemoteAgentStub remoteAgentStub = new RemoteAgentStub(DESCRIPTOR) {
@Override
public void setRemoteObject(String param) throws RemoteException {
showTips(RemoteAbility.this, param);
if(isConnected) {
showTips(RemoteAbility.this, param);
}
}
};
private boolean isConnected = false;
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
@ -60,12 +63,14 @@ public class RemoteAbility extends Ability {
@Override
protected IRemoteObject onConnect(Intent intent) {
showTips(RemoteAbility.this, "RemoteService onConnect");
isConnected = true;
return remoteAgentStub;
}
@Override
protected void onDisconnect(Intent intent) {
super.onDisconnect(intent);
isConnected = false;
showTips(RemoteAbility.this, "RemoteService onDisconnect");
}

View File

@ -114,19 +114,29 @@ public class MainAbilitySlice extends AbilitySlice implements IAbilityContinuati
Component continueFAButton = findComponentById(ResourceTable.Id_continue_fa_button);
connectRemotePAButton.setClickedListener(component -> connectService());
disconnectRemotePAButton.setClickedListener(component -> disConnectAbility());
continueFAButton.setClickedListener(component -> continueAbility(selectDeviceId));
continueFAButton.setClickedListener(component -> continueFA());
text = (Text) findComponentById(ResourceTable.Id_text);
text.setText(param);
}
private void continueFA() {
try {
continueAbility(selectDeviceId);
} catch (IllegalArgumentException e) {
HiLog.info(LABEL_LOG, e.getMessage());
}
}
private void disConnectAbility() {
disconnectAbility(connection);
try {
DeviceManager.unInitDistributedEnvironment(selectDeviceId,iInitCallback);
} catch (RemoteException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
HiLog.info(LABEL_LOG, e.getMessage());
}
remoteAgentProxy = null;
}