背景
有时候我们需要在app启动时去获取用户复制的内容,这时候需要进行剪切板相关操作,而Android Q开始只有系统默认的输入法应用或者应用处于焦点时才能获取到剪切板的数据,经测试只有targetSdkVersion和设备型号均为Android Q以上时此规则才会生效。
原来获取剪切板的操作如下:
ClipboardManager clipboardManager = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = clipboardManager.getPrimaryClip();
现在Android Q的api说明如下
/**
* Returns the current primary clip on the clipboard.
*
* If the application is not the default IME or does not have input focus this return
* {@code null}.
*
* @see #setPrimaryClip(ClipData)
*/
public @Nullable ClipData getPrimaryClip(){
try {
return mService.getPrimaryClip(mContext.getOpPackageName(), mContext.getUserId());
} catch (RemoteException e) {
throw e.rethrowFrom