跨设备访问DataAbility的坑点!
注意!!!访问远程设备的Data Ability除了上面讲的uri里添加DevcieId以外,必须注意下面几点:
- 要定义应用级别的访问Data Abiltiy的权限
"defPermissions": [
{ "name": "cn.ybzy.hmsdemo.DataAbilityShellProvider.PROVIDER", "grantMode": "system_grant" }
]
- 要加分布式数据同步和设备信息获取权限
"reqPermissions": [
{ "name": "ohos.permission.GET_DISTRIBUTED_DEVICE_INFO" },
{ "name": "ohos.permission.DISTRIBUTED_DATASYNC" },
{ "name": "ohos.permission.READ_USER_STORAGE" },
{ "name": "cn.ybzy.hmsdemo.DataAbilityShellProvider.PROVIDER" }
]
requestPermissionsFromUser(new String[]{"ohos.permission.DISTRIBUTED_DATASYNC", "ohos.permission.servicebus.ACCESS_SERVICE", "com.huawei.hwddmp.servicebus.BIND_SERVICE"}, 0);
- Data Ability的注册信息中一定要打开允许其他应用访问的权限
{
"visible": true, //最后的坑,创建Data Ability时候不会自动添加
"permissions": [ "cn.ybzy.hmsdemo.DataAbilityShellProvider.PROVIDER" ],
"name": "cn.ybzy.hmsdemo.SimpleDataAbility",
"icon": "$media:icon",
"description": "hap sample empty provider",
"type": "data", "uri": "dataability://cn.ybzy.hmsdemo.SimpleDataAbility"
}
作者:zhonghongfa
想了解更多内容,请访问51CTO和华为合作共建的鸿蒙社区:https://harmonyos.51cto.com