getPrimaryClip() 为空 null
2024-01-19 10:56:37
michael007js
137
getPrimaryClip() 为空 null
当APP打开状态的时候,直接获取剪切板getPrimaryClip() 为空。
可以在 onWindowFocusChanged() 调取
可以延迟获取 postDelay 取拿
以下以onWindowFocusChanged举例:
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { ClipboardManager clipManager = (ClipboardManager) getContext().getSystemService(CLIPBOARD_SERVICE); if (clipManager == null || !clipManager.hasPrimaryClip()) { return; } //如果是文本信息 if (clipManager != null && clipManager.getPrimaryClipDescription() != null && clipManager.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) { ClipData clip = clipManager.getPrimaryClip(); if (clip != null) { ClipData.Item item = clip.getItemAt(0); if (item.getText() != null) { String content = item.getText().toString(); //做你该做的事情 //TODO //清空粘贴板 clipManager.setText(""); } } } } }
参考:https://stackoverflow.com/questions/66079485/how-to-read-android-clipboard-text-when-opening-an-app