How to trigger an event, for example, launching an application when selecting text

I was wondering if I can run an action or application when the text is selected in any application, such as a browser, messages, etc.

Just like we select text anywhere where a small pop-up window appears with the cut, copy, paste options mentioned. can i add another button there? to run my application?

if I can direct me, how can I do this and send the data to my application.

Thanks!

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); 
+6
source share
2 answers

Closest to what you describe will be registered for your application as an intent processing android.intent.action.SEND , as described here:

http://developer.android.com/training/sharing/receive.html

The intent-filter declaration will look something like this:

 <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> 

What the user sees ...

When the user is in another application and selects the text, if it is supported by the application, they will receive the copy and paste options that you have already seen, but they will also receive the option โ€œshareโ€ - an icon of three dots connected by two lines:

enter image description here

... and when the user then clicks on the Share icon:

Your application will appear in the list of applications that are displayed to the user. If the user selects your application, you will receive the intent with a common text that you can extract, that is:

String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);

Further reading

http://android-developers.blogspot.com/2012/02/share-with-intents.html

+5
source

Android 6.0 Marshmallow introduced ACTION_PROCESS_TEXT . It allows you to add custom actions to the text selection toolbar.

First, you must add an intent filter to your manifest,

 <activity android:name=".YourActivity" android:label="@string/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> 

then in YourActivity , which you want to run when the user selects the text.

 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 } 

A source

+3
source

Source: https://habr.com/ru/post/943887/


All Articles