Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

Clipboard in Android 10 is not Working as expected

I've implemented a copy from clipboard feature. So, whenever user copies something and my app is resumed after that I show a Snackbar to perform some action. It is working fine in Android 9 but in Android 10 as per policy changes it is not observing clipboard changes in background. That's ok but when I call following method in onResume it doesn't get any text as hasPrimaryClip is false. But on the same screen if I call same method on any button click then it is working fine and returning copied text. May be clipboard doesn't give access immediately after onResume and with some delay when any button is clicked it allows access. What could be the issue? Any ideas would be highly appreciable.

Thanks

public String readFromClipboard() {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboard.hasPrimaryClip()) {
            android.content.ClipDescription description = clipboard.getPrimaryClipDescription();
            android.content.ClipData data = clipboard.getPrimaryClip();
            if (data != null && description != null && description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
                return String.valueOf(data.getItemAt(0).getText());
        }
        return null;
    }
question from:https://stackoverflow.com/questions/65949302/clipboard-in-android-10-is-not-working-as-expected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What I've observed is that we can access clipboard data in 'onWindowFocusChanged(boolean hasFocus)' method if hasFocus is true. This method is called approx. ~100ms later than onResume. So, copying by clicking on a button was working fine because app already had focus.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...