启动远程PageAbility(仅对系统应用开放)
启动远程PageAbility同样通过featureAbility中的startAbility接口实现。
除引入'@ohos.ability.featureAbility'外,还需引入'@ohos.distributedHardware.deviceManager',通过DeviceManager(该组件提供帐号无关的分布式设备的认证组网能力)的getTrustedDeviceListSync接口(获取信任设备列表)获取远端的deviceId,写入want中,用于启动远程PageAbility。
由于当前DeviceManager的getTrustedDeviceListSync接口仅对系统应用开放,故现阶段非系统应用无法获取其他设备信息,无远程启动设备选择入口,远程启动Ability开发。
表1 featureAbility接口说明
接口名 | 接口描述 ** 开发前请熟悉鸿蒙开发指导文档**:[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md ]点击或者复制转到。 |
---|---|
startAbility(parameter: StartAbilityParameter) | 启动Ability。 |
startAbilityForResult(parameter: StartAbilityParameter) | 启动Ability,并在该Ability被销毁时返回执行结果。 |
表2 deviceManager接口说明
接口名 | 接口描述 |
---|---|
getTrustedDeviceListSync(): Array | 同步获取所有可信设备列表。 |
在跨设备场景下,启动远程PageAbility首先需要向用户申请数据同步的权限,相关接口说明如下:
表3 AtManager接口说明
接口名 | 接口描述 |
---|---|
checkAccessToken(tokenID: number, permissionName: string) : Promise | 校验应用是否授予权限。使用Promise异步回调。返回值GrantStatus。建议使用checkAccessToken代替verifyAccessToken(已废弃),verifyAccessToken从API version 9开始不再维护。 |
表4 context接口说明
接口名 | 接口描述 |
---|---|
requestPermissionsFromUser(permissions: Array, requestCode: number, resultCallback: AsyncCallback< PermissionRequestResult>): void | 以callback形式从系统请求某些权限,详见对应[接口文档]。 |
如下示例代码展示了向用户申请数据同步权限的方法:
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() {
//...
}
}
在获取数据同步权限后,需要获取可信设备列表,进行设备选择。
如下示例展示了通过getAvailableDeviceListSync获取可信设备列表,选择设备的方法。
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直接拿`
设备选择完成后,通过调用startAbility接口,显式启动远程PageAbility。
如下示例展示了通过startAbility显式启动远程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() {
//...
}
}
审核编辑 黄宇
-
接口
+关注
关注
33文章
8593浏览量
151123 -
鸿蒙
+关注
关注
57文章
2350浏览量
42842
发布评论请先 登录
相关推荐
评论