Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire an event like launch an application when a text is selected

I was wondering if i can launch an activity or application when a text is selected in any application like browser, messages etc.

Like when we select a text at any where a small pop-up appears mentioning cut, copy, paste option. can i add another button there? to launch my application?

if i can please guide me how can i do that and send data to my application..

Thank you !

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);
like image 699
Saqib Vohra Avatar asked Apr 29 '13 14:04

Saqib Vohra


1 Answers

The closest thing to what you're describing would be for your app to register as handling the android.intent.action.SEND intent, as described here:

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

The intent-filter declaration would 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 a user is in some other app and selects text, if it's supported by the app they'll get the copy & paste options you've already seen, but they'll also get the 'share' option - the icon is three dots connected by two lines:

enter image description here

...and when the user then taps on the 'Share' icon:

Your app will appear in the list of apps that are displayed to the user. If the user selects your app, you'll receive an intent with the shared text which you can then extract, ie:

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

Further reading

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

like image 58
Roberto Tyley Avatar answered Sep 18 '22 17:09

Roberto Tyley