ClipboardManager does not work on Android Q (Smart Solution!)
Unless your app is the default input method editor (IME) or is the app that currently has focus, your app cannot access clipboard data.
But you do not want to make your app (IME), is there any solution?

Creating custom Text Selection actions with ACTION_PROCESS_TEXT
Android 6.0 Marshmallow introduced a new floating text selection toolbar, which brings the standard text selection actions, like cut, copy, and paste, closer to the text you’ve selected. Even better though is the new ACTION_PROCESS_TEXT which makes it possible for any app to add custom actions to that text selection toolbar.

How to create custom Text Selection action in my app?
1- Add an intent filter to an Activity in your manifest:
<activity
android:name=".ProcessTextActivity"
android:label="@string/process_text_action_name">
<intent-filter>
<action android:name="android.intent.action.PROCESS_TEXT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>Note: don’t add android:exported=”false”, if you put it your action will still appear in other apps, but they’ll get a SecurityException immediately upon clicking it.
2- Getting the selected text:
Therefore your onCreate() method may look something like:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.process_text_main);
CharSequence text = getIntent()
.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT);
// process the text
}3- Returning a result:
There’s one other extra included in the ACTION_PROCESS_TEXT Intent though: EXTRA_PROCESS_TEXT_READONLY. This boolean extra denotes whether the selected text you just received can be edited by the user (such as would be the case in an EditText).
You’d retrieve the extra with code such as
boolean readonly = getIntent()
.getBooleanExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, false);How to handle text selection action without open the activity?
In the fact no way to do that because the system only looks for Activities that contain the correct intent filter. but that doesn’t mean you can’t have your Activity launch a Service using a theme of Theme.Translucent.NoTitleBar or even Theme.NoDisplay (as long as you immediately finish the Activity), but make sure you have some user visible hint that their action was received — a notification starting, a Toast, etc.
1- Create Transparent style:
<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>2- Create DummyActivity:
<activity android:name=".DummyActivity"
android:label="@string/app_name"
android:label="@string/process_text_action_name"
android:theme="@style/Theme.Transparent">
<intent-filter>
<action android:name="android.intent.action.PROCESS_TEXT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>3- Handle process text
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dummy);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
CharSequence text = getIntent()
.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT);
// show notification, Toast, etc.
} finish() // close DummyActivity}
This is the best solution you can do now for android Q, maybe in the next Android version will found a new permission to allow the app to read the data from Clipboard.
