啟動遠(yuǎn)程PageAbility(僅對系統(tǒng)應(yīng)用開放)
啟動遠(yuǎn)程PageAbility同樣通過featureAbility中的startAbility接口實現(xiàn)。
除引入'@ohos.ability.featureAbility'外,還需引入'@ohos.distributedHardware.deviceManager',通過DeviceManager(該組件提供帳號無關(guān)的分布式設(shè)備的認(rèn)證組網(wǎng)能力)的getTrustedDeviceListSync接口(獲取信任設(shè)備列表)獲取遠(yuǎn)端的deviceId,寫入want中,用于啟動遠(yuǎn)程PageAbility。
由于當(dāng)前DeviceManager的getTrustedDeviceListSync接口僅對系統(tǒng)應(yīng)用開放,故現(xiàn)階段非系統(tǒng)應(yīng)用無法獲取其他設(shè)備信息,無遠(yuǎn)程啟動設(shè)備選擇入口,遠(yuǎn)程啟動Ability開發(fā)。
表1 featureAbility接口說明
接口名 | 接口描述 ** 開發(fā)前請熟悉鴻蒙開發(fā)指導(dǎo)文檔**:[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md ]點(diǎn)擊或者復(fù)制轉(zhuǎn)到。 |
---|---|
startAbility(parameter: StartAbilityParameter) | 啟動Ability。 |
startAbilityForResult(parameter: StartAbilityParameter) | 啟動Ability,并在該Ability被銷毀時返回執(zhí)行結(jié)果。 |
表2 deviceManager接口說明
接口名 | 接口描述 |
---|---|
getTrustedDeviceListSync(): Array | 同步獲取所有可信設(shè)備列表。 |
在跨設(shè)備場景下,啟動遠(yuǎn)程PageAbility首先需要向用戶申請數(shù)據(jù)同步的權(quán)限,相關(guān)接口說明如下:
表3 AtManager接口說明
接口名 | 接口描述 |
---|---|
checkAccessToken(tokenID: number, permissionName: string) : Promise | 校驗應(yīng)用是否授予權(quán)限。使用Promise異步回調(diào)。返回值GrantStatus。建議使用checkAccessToken代替verifyAccessToken(已廢棄),verifyAccessToken從API version 9開始不再維護(hù)。 |
表4 context接口說明
接口名 | 接口描述 |
---|---|
requestPermissionsFromUser(permissions: Array, requestCode: number, resultCallback: AsyncCallback< PermissionRequestResult>): void | 以callback形式從系統(tǒng)請求某些權(quán)限,詳見對應(yīng)[接口文檔]。 |
如下示例代碼展示了向用戶申請數(shù)據(jù)同步權(quán)限的方法:
import abilityAccessCtrl from "@ohos.abilityAccessCtrl";
import featureAbility from '@ohos.ability.featureAbility';
import bundle from '@ohos.bundle.bundleManager';
import hilog from '@ohos.hilog';
const TAG: string = 'PagePageAbilitySecond'
const domain: number = 0xFF00;
@Entry
@Component
struct PagePageAbilitySecond {
async requestPermission(): Promise< void > {
hilog.info(domain, TAG, 'RequestPermission begin');
let array: Array< string > = ['ohos.permission.DISTRIBUTED_DATASYNC'];
let bundleFlag = 0;
let tokenID: number | undefined = undefined;
let userID = 100;
let appInfo = await bundle.getApplicationInfo('com.samples.famodelabilitydevelop', bundleFlag, userID);
tokenID = appInfo.accessTokenId;
let atManager = abilityAccessCtrl.createAtManager();
let requestPermissions: Array< string > = [];
for (let i = 0;i < array.length; i++) {
let result = await atManager.verifyAccessToken(tokenID, array[i]);
hilog.info(domain, TAG, 'checkAccessToken result:' + JSON.stringify(result));
if (result != abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) {
requestPermissions.push(array[i]);
}
}
hilog.info(domain, TAG, 'requestPermissions:' + JSON.stringify(requestPermissions));
if (requestPermissions.length == 0) {
return;
}
let context = featureAbility.getContext();
context.requestPermissionsFromUser(requestPermissions, 1, (error, data) = > {
hilog.info(domain, TAG, 'error:' + error.message + ',data:' + JSON.stringify(data));
hilog.info(domain, TAG, 'data requestCode:' + data.requestCode);
hilog.info(domain, TAG, 'data permissions:' + data.permissions);
hilog.info(domain, TAG, 'data authResults:' + data.authResults);
});
hilog.info(domain, TAG, 'RequestPermission end');
}
build() {
//...
}
}
在獲取數(shù)據(jù)同步權(quán)限后,需要獲取可信設(shè)備列表,進(jìn)行設(shè)備選擇。
如下示例展示了通過getAvailableDeviceListSync獲取可信設(shè)備列表,選擇設(shè)備的方法。
import deviceManager from '@ohos.distributedDeviceManager';
import promptAction from '@ohos.promptAction';
import hilog from '@ohos.hilog';
const TAG: string = 'PagePageAbilitySecond'
const domain: number = 0xFF00;
@Entry
@Component
struct PagePageAbilitySecond {
@State deviceID: string = '';
getRemoteDeviceId(): void {
let dmClass: deviceManager.DeviceManager;
dmClass = deviceManager.createDeviceManager('com.samples.famodelabilitydevelop');
try {
if (typeof dmClass === 'object' && dmClass !== null) {
let list = dmClass.getAvailableDeviceListSync();
if (typeof (list) == undefined || list.length == 0) {
hilog.info(domain, TAG, 'EntryAbility onButtonClick getRemoteDeviceId err: list is null');
return;
}
hilog.info(domain, TAG, `EntryAbility onButtonClick getRemoteDeviceId success[${list.length}]:` + JSON.stringify(list[0]));
if (list[0].networkId != undefined) {
this.deviceID = list[0].networkId;
}
promptAction.showToast({
message: this.deviceID
});
} else {
hilog.info(domain, TAG, 'EntryAbility onButtonClick getRemoteDeviceId err: dmClass is null');
}
} catch (error) {
hilog.info(domain, TAG, `getRemoteDeviceId error, error=${error}, message=${error.message}`);
}
}
build() {
//...
}
}
`HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿`
設(shè)備選擇完成后,通過調(diào)用startAbility接口,顯式啟動遠(yuǎn)程PageAbility。
如下示例展示了通過startAbility顯式啟動遠(yuǎn)程PageAbility的方法。
import featureAbility from '@ohos.ability.featureAbility';
import Want from '@ohos.app.ability.Want';
import promptAction from '@ohos.promptAction';
import { BusinessError } from '@ohos.base';
import hilog from '@ohos.hilog';
const TAG: string = 'PagePageAbilitySecond'
const domain: number = 0xFF00;
@Entry
@Component
struct PagePageAbilitySecond {
@State deviceID: string = '';
onStartRemoteAbility(): void {
hilog.info(domain, TAG, 'onStartRemoteAbility begin');
let wantValue: Want = {
bundleName: 'ohos.samples.distributedmusicplayer',
abilityName: 'ohos.samples.distributedmusicplayer.MainAbility',
deviceId: this.deviceID, // this.deviceID的獲取方式在前面的示例代碼中
};
hilog.info(domain, TAG, 'onStartRemoteAbility want=' + JSON.stringify(wantValue));
featureAbility.startAbility({
want: wantValue
}).then((data) = > {
promptAction.showToast({
message: $r('app.string.start_remote_success_toast')
});
hilog.info(domain, TAG, 'onStartRemoteAbility finished, ' + JSON.stringify(data));
}).catch((error: BusinessError) = > {
promptAction.showToast({
message: JSON.stringify(error)
});
hilog.error(domain, TAG, 'onStartRemoteAbility failed: ' + JSON.stringify(error));
});
hilog.info(domain, TAG, 'onStartRemoteAbility end');
}
build() {
//...
}
}
審核編輯 黃宇
-
接口
+關(guān)注
關(guān)注
33文章
8611瀏覽量
151239 -
鴻蒙
+關(guān)注
關(guān)注
57文章
2358瀏覽量
42871
發(fā)布評論請先 登錄
相關(guān)推薦
評論