1、HarmonyOS 粘贴板权限无法申请时,粘贴板能力如何与跨端容器如H5、Flutter之间进行共享?
粘贴板权限无法申请时,粘贴板能力如何与跨端容器如H5、Flutter之间进行共享?
可以在h5页面传递订单id到arkTs处,然后通过arkTs复制到粘贴板上面,两者数据交互参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/web-in-page-app-function-invoking-V5
arkTs的复制粘贴功能参考demo:
import { pasteboard } from '@kit.BasicServicesKit';
@State message: string = 'Hello World'
Text(this.message).onClick(() => {
const pasteboardData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, this.message)
const systemPasteboard = pasteboard.getSystemPasteboard()
systemPasteboard.setData(pasteboardData) //将数据放入剪切板
systemPasteboard.getData().then((data) => {
if (data) {
promptAction.showToast({ message: "复制成功" })
} else {
promptAction.showToast({ message: "复制失败" })
}
})
})
2、HarmonyOS 使用async 、await 的含义?
使用async 、await 时,整个异步调用是在主线程吗? 这过程中有没有发生线程的切换?
使用async 、await时还是在主线程中,没有发生过线程切换,目前子线程只能是taskpool和worker
3、HarmonyOS requestPermissionsFromUser 用户点了允许了之后,getBundleInfoForSelf返回的BundleInfo里,permissionGrantStates里还是-1?
参考以下代码:
async reqPermissionsFromUser(): Promise<number[]> {
let context = getContext() as common.UIAbilityContext;
let atManager = abilityAccessCtrl.createAtManager();
let grantStatus = await atManager.requestPermissionsFromUser(context, ['ohos.permission.CAMERA']);
return grantStatus.authResults;
}
// 申请相机权限
async requestCameraPermission() {
let grantStatus = await this.reqPermissionsFromUser()
for (let i = 0; i < grantStatus.length; i++) {
if (grantStatus[i] === 0) {
// 用户授权,可以继续访问目标操作
this.userGrant = true;
}
}
}
async onPageShow() {
await this.requestCameraPermission();
}
4、HarmonyOS 如何获取锁屏事件?
视频播放中需要监听锁屏事件,锁屏后需要暂停播放,现无法获取锁屏事件。
参考demo:
import Base from '@ohos.base';
import CommonEventManager from '@ohos.commonEventManager';
let subscriber:CommonEventManager.CommonEventSubscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
//订阅者信息
let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
events: [CommonEventManager.Support.COMMON_EVENT_SCREEN_LOCKED, CommonEventManager.Support.COMMON_EVENT_SCREEN_UNLOCKED]
};
//发布公共事件回调
function subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) {
if (err) {
console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
} else {
if (data.event == CommonEventManager.Support.COMMON_EVENT_SCREEN_LOCKED) {
console.info("MWB ",`lock screen`);
} else if (data.event == CommonEventManager.Support.COMMON_EVENT_SCREEN_UNLOCKED) {
console.info("MWB ",`unlock screen`);
}
}
}
//创建订阅者回调
function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
if(!err) {
console.info("createSubscriber");
subscriber = commonEventSubscriber;
try {
CommonEventManager.subscribe(subscriber, subscribeCB);
} catch (error) {
let err:Base.BusinessError = error as Base.BusinessError;
console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
}
} else {
console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
}
}
//创建订阅者
try {
CommonEventManager.createSubscriber(subscribeInfo, createCB);
} catch (error) {
let err:Base.BusinessError = error as Base.BusinessError;
console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
}
@Entry
@Component
struct ScreenLockEventManageDemo {
build() {
Column() {
Text('HELLO WORLD')
.fontSize('20')
}
.width('100%')
.height('100%')
}
}
5、HarmonyOS AlertDialog如何与页面做绑定,关闭页面的同时关闭所有的AlertDialog?
AlertDialog只能设置autoCancel点击时取消,或者在点击定义的按钮时取消,如果想通过方法调用取消的话可以尝试使用自定义弹窗CustomDialog
AlertDialog参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-methods-alert-dialog-box-V5
CustomDialog参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-methods-custom-dialog-box-V5
1、HarmonyOS 粘贴板权限无法申请时,粘贴板能力如何与跨端容器如H5、Flutter之间进行共享?
粘贴板权限无法申请时,粘贴板能力如何与跨端容器如H5、Flutter之间进行共享?
可以在h5页面传递订单id到arkTs处,然后通过arkTs复制到粘贴板上面,两者数据交互参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/web-in-page-app-function-invoking-V5
arkTs的复制粘贴功能参考demo:
import { pasteboard } from '@kit.BasicServicesKit';
@State message: string = 'Hello World'
Text(this.message).onClick(() => {
const pasteboardData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, this.message)
const systemPasteboard = pasteboard.getSystemPasteboard()
systemPasteboard.setData(pasteboardData) //将数据放入剪切板
systemPasteboard.getData().then((data) => {
if (data) {
promptAction.showToast({ message: "复制成功" })
} else {
promptAction.showToast({ message: "复制失败" })
}
})
})
2、HarmonyOS 使用async 、await 的含义?
使用async 、await 时,整个异步调用是在主线程吗? 这过程中有没有发生线程的切换?
使用async 、await时还是在主线程中,没有发生过线程切换,目前子线程只能是taskpool和worker
3、HarmonyOS requestPermissionsFromUser 用户点了允许了之后,getBundleInfoForSelf返回的BundleInfo里,permissionGrantStates里还是-1?
参考以下代码:
async reqPermissionsFromUser(): Promise<number[]> {
let context = getContext() as common.UIAbilityContext;
let atManager = abilityAccessCtrl.createAtManager();
let grantStatus = await atManager.requestPermissionsFromUser(context, ['ohos.permission.CAMERA']);
return grantStatus.authResults;
}
// 申请相机权限
async requestCameraPermission() {
let grantStatus = await this.reqPermissionsFromUser()
for (let i = 0; i < grantStatus.length; i++) {
if (grantStatus[i] === 0) {
// 用户授权,可以继续访问目标操作
this.userGrant = true;
}
}
}
async onPageShow() {
await this.requestCameraPermission();
}
4、HarmonyOS 如何获取锁屏事件?
视频播放中需要监听锁屏事件,锁屏后需要暂停播放,现无法获取锁屏事件。
参考demo:
import Base from '@ohos.base';
import CommonEventManager from '@ohos.commonEventManager';
let subscriber:CommonEventManager.CommonEventSubscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
//订阅者信息
let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
events: [CommonEventManager.Support.COMMON_EVENT_SCREEN_LOCKED, CommonEventManager.Support.COMMON_EVENT_SCREEN_UNLOCKED]
};
//发布公共事件回调
function subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) {
if (err) {
console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
} else {
if (data.event == CommonEventManager.Support.COMMON_EVENT_SCREEN_LOCKED) {
console.info("MWB ",`lock screen`);
} else if (data.event == CommonEventManager.Support.COMMON_EVENT_SCREEN_UNLOCKED) {
console.info("MWB ",`unlock screen`);
}
}
}
//创建订阅者回调
function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
if(!err) {
console.info("createSubscriber");
subscriber = commonEventSubscriber;
try {
CommonEventManager.subscribe(subscriber, subscribeCB);
} catch (error) {
let err:Base.BusinessError = error as Base.BusinessError;
console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
}
} else {
console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
}
}
//创建订阅者
try {
CommonEventManager.createSubscriber(subscribeInfo, createCB);
} catch (error) {
let err:Base.BusinessError = error as Base.BusinessError;
console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
}
@Entry
@Component
struct ScreenLockEventManageDemo {
build() {
Column() {
Text('HELLO WORLD')
.fontSize('20')
}
.width('100%')
.height('100%')
}
}
5、HarmonyOS AlertDialog如何与页面做绑定,关闭页面的同时关闭所有的AlertDialog?
AlertDialog只能设置autoCancel点击时取消,或者在点击定义的按钮时取消,如果想通过方法调用取消的话可以尝试使用自定义弹窗CustomDialog
AlertDialog参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-methods-alert-dialog-box-V5
CustomDialog参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-methods-custom-dialog-box-V5