HarmonyOS 剪切板systemPasteboard.getData接口获取不到数据

升级版本后发现systemPasteboard.getData接口获取不到数据;在旧版本上没有问题。也加上ohos.permission.READ_PASTEBOARD权限了。

HarmonyOS
2024-08-02 15:22:22
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
牙膏只用咸的

涉及接口变更,需要弹窗授权,详情参考:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-basic-services-kit/js-apis-pasteboard.md#getdata9

参考以下代码

import hilog from '@ohos.hilog'; 
import { BusinessError } from '@ohos.base'; 
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl'; 
import common from '@ohos.app.ability.common'; 
import bundleManager from '@ohos.bundle.bundleManager'; 
import pasteboard from '@ohos.pasteboard'; 
 
 
async function checkAccessToken(permission: Permissions): Promise<abilityAccessCtrl.GrantStatus> { 
  let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); 
  let grantStatus: abilityAccessCtrl.GrantStatus = abilityAccessCtrl.GrantStatus.PERMISSION_DENIED; 
 
  // 获取应用程序的accessTokenID 
  let tokenId: number = 0; 
  try { 
    let bundleInfo: bundleManager.BundleInfo = await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION); 
    let appInfo: bundleManager.ApplicationInfo = bundleInfo.appInfo; 
    tokenId = appInfo.accessTokenId; 
  } catch (error) { 
    let err: BusinessError = error as BusinessError; 
    console.error(`Failed to get bundle info for self. Code is ${err.code}, message is ${err.message}`); 
  } 
 
  // 校验应用是否被授予权限 
  try { 
    grantStatus = await atManager.checkAccessToken(tokenId, permission); 
  } catch (error) { 
    let err: BusinessError = error as BusinessError; 
    console.error(`Failed to check access token. Code is ${err.code}, message is ${err.message}`); 
  } 
 
  return grantStatus; 
} 
 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
  @State msgList: Array<string> = []; 
  // @State oaid: string = ''; 
  @State permission_state: boolean = true; 
 
  reqPermissionsFromUser(permissions: Array<Permissions>): void { 
    let context = getContext(this) as common.UIAbilityContext; 
    let atManager = abilityAccessCtrl.createAtManager(); 
    // requestPermissionsFromUser会判断权限的授权状态来决定是否唤起弹窗 
    atManager.requestPermissionsFromUser(context, permissions).then((data) => { 
    let grantStatus: Array<number> = data.authResults; 
    let length: number = grantStatus.length; 
    for (let i = 0; i < length; i++) { 
    if (grantStatus[i] === 0) { 
    // 用户授权,可以继续访问目标操作 
    this.permission_state = true; 
    this.msgList.push('申请权限成功'); 
  } 
  else { 
    // 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限 
    // openPermissionsInSystemSettings(); 
    console.error("user did not grant!") 
    this.permission_state = false; 
    this.msgList.push('申请权限失败'); 
  } 
} 
// 授权成功 
}).catch((err: String) => { 
}) 
} 
 
async getPaste() { 
  const permissions: Array<Permissions> = ['ohos.permission.READ_PASTEBOARD']; 
  let grantStatus1: abilityAccessCtrl.GrantStatus = await checkAccessToken(permissions[0]); 
 
  if (grantStatus1 === abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) { 
    try { 
      let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 
      systemPasteboard.getData((err: BusinessError, pasteData: pasteboard.PasteData) => { 
        if (err) { 
          console.error('Failed to get PasteData. Cause: ' + err.message); 
          return; 
        } 
        let text: string = pasteData.getPrimaryText(); 
        this.msgList.push('剪贴板内容:'+text); 
      }); 
    } catch (err) { 
      hilog.error(0x0000, 'testTag', '%{public}s', `get oaid by promise catch error: ${err.code} ${err.message}`); 
    } 
  } else { 
    // 申请权限 
    if(this.permission_state) { 
      this.reqPermissionsFromUser(permissions) 
    }else{ 
 
    } 
  } 
} 
 
build() { 
  Column() { 
    Button({ type: ButtonType.Normal, stateEffect: true }) { 
      Text('读取剪贴板内容') 
        .fontColor(Color.White) 
        .fontSize(20) 
    } 
    .borderRadius(8) 
      .width('45%') 
      .height('5%') 
      .backgroundColor(0x317aff) 
      .onClick(() => { 
        this.getPaste() 
        try { 
          let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 
          systemPasteboard.getData((err: BusinessError, pasteData: pasteboard.PasteData) => { 
            if (err) { 
              console.error('Failed to get PasteData. Cause: ' + err.message); 
              return; 
            } 
            let text: string = pasteData.getPrimaryText(); 
            this.msgList.push('剪贴板内容:'+text); 
          }); 
        } catch (err) { 
          hilog.error(0x0000, 'testTag', '%{public}s', `get oaid by promise catch error: ${err.code} ${err.message}`); 
        } 
      }) 
    ForEach(this.msgList, (item: string) => { 
      Text(item) 
        .fontSize(25) 
        .textOverflow({ overflow: TextOverflow.Clip }) 
        .maxLines(10) 
        .width('80%') 
        .margin({ top: 5 }) 
    }) 
  } 
  .width('100%') 
    .height('100%') 
} 
}
分享
微博
QQ
微信
回复
2024-08-02 20:31:10
相关问题
复制图片到剪切板获取剪切板消息
632浏览 • 1回复 待解决
HarmonyOS 如何获取剪切板的内容
243浏览 • 1回复 待解决
是否支持在应用后台获取剪切板数据
219浏览 • 1回复 待解决
系统剪切板功能的使用
992浏览 • 1回复 待解决
HarmonyOS 怎么把内容复制到剪切板
331浏览 • 1回复 待解决
JS FA调用 java PA,取到参数剪切板问题
5060浏览 • 1回复 待解决
如何将一段文字保存到剪切板中?
217浏览 • 1回复 待解决
http request 请求不到接口数据
4755浏览 • 1回复 待解决
Scrapy 获取不到拉勾网指定的xpath数据
5307浏览 • 0回复 待解决
请问怎么获取HarmonyOS的开发
17717浏览 • 8回复 待解决
HarmonyOS 获取不到手机号
79浏览 • 1回复 待解决
如何实现页面加载前从接口获取数据
2285浏览 • 1回复 待解决
Preferences获取不到
8836浏览 • 2回复 待解决
native侧的log获取不到
1511浏览 • 1回复 待解决