diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 124592db955713a79ffd5c4b4f95d1dc1daff28a..fcbb37b1da010d9ea6700392d527327391454871 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -23,6 +23,8 @@ export default class EntryAbility extends UIAbility { onContinue(wantParam: Record) { Logger.info(wantParam.version.toString(), wantParam.targetDevice.toString()); + // Get and save the target device info + AppStorage.setOrCreate('targetDevice', wantParam.targetDevice.toString()); // Preparing to Migrate Data let activeLive: number = AppStorage.get('activeLive') as number; @@ -38,6 +40,9 @@ export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { Logger.info('Ability onCreate'); + if (want.parameters?.deviceId !== undefined) { + AppStorage.setOrCreate('targetDevice', want.parameters?.deviceId.toString()); + }; this.checkPermissions(); // If the invoking reason is migration, set the status to migratable to cope with cold start (ensuring migration continuity) @@ -62,6 +67,9 @@ export default class EntryAbility extends UIAbility { onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) { Logger.info('Ability onCreate'); + if (want.parameters?.deviceId !== undefined) { + AppStorage.setOrCreate('targetDevice', want.parameters?.deviceId.toString()); + }; // If the invoking reason is migration, set the status to migratable to cope with hot start (ensuring migration continuity) if (launchParam.launchReason === AbilityConstant.LaunchReason.CONTINUATION) { diff --git a/entry/src/main/ets/pages/CommentPage.ets b/entry/src/main/ets/pages/CommentPage.ets index c3747aa05980ba0f1fabf3c0fd77b5879149a2e9..fa4340fd0e2fdc8c21051888805c2a3423d24464 100644 --- a/entry/src/main/ets/pages/CommentPage.ets +++ b/entry/src/main/ets/pages/CommentPage.ets @@ -22,7 +22,7 @@ import { LiveData } from '../model/LiveData'; import Constants from '../common/Constants'; import Logger from '../utils/Logger'; -function getRemoteDeviceId(): string | undefined { +function getRemoteDeviceId(targetDevice: string): string | undefined { let dmClass = distributedDeviceManager.createDeviceManager('com.example.codelab'); if (typeof dmClass === 'object' && dmClass !== null) { let list = dmClass.getAvailableDeviceListSync(); @@ -34,7 +34,12 @@ function getRemoteDeviceId(): string | undefined { Logger.error('getRemoteDeviceId err: list is null'); return; } - Logger.info(list[0].networkId as string); + for (const device of list) { + if (targetDevice !== '' && device.networkId === targetDevice) { + Logger.info(device.networkId as string); + return device.networkId; + } + } return list[0].networkId; } else { Logger.error('getRemoteDeviceId err: dmClass is null'); @@ -49,6 +54,7 @@ export struct CommentPage { @State isPhone: boolean = false; @State @Watch("onLiveListUpdated") liveList: Array = []; @Link @Watch("onActiveUpdated") activeItem: number; + @StorageProp('targetDevice') targetDevice: string = ''; scroller: Scroller = new Scroller(); listenerIsPhone = this.getUIContext().getMediaQuery().matchMediaSync('(orientation:landscape)'); private context = this.getUIContext().getHostContext() as common.UIAbilityContext; @@ -155,7 +161,7 @@ export struct CommentPage { .visibility(deviceInfo.deviceType !== 'phone' ? Visibility.Visible : Visibility.None) .onClick(() => { let want: Want = { - deviceId: getRemoteDeviceId(), + deviceId: getRemoteDeviceId(this.targetDevice), bundleName: 'com.example.codelab', abilityName: 'InputAbility', };