7

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);
Saqib Vohra
  • 366
  • 7
  • 17
  • possible duplicate of [How do I programmatically launch a specific application in Android?](http://stackoverflow.com/questions/3343432/how-do-i-programmatically-launch-a-specific-application-in-android) – waqaslam Apr 29 '13 at 14:44
  • how do i "select" a text? – bofredo Apr 29 '13 at 14:50
  • Thats not answer to my question. My question is that whenever i select text in any application like while messaging i select text, it should prompt me an option to launch my app and send that highlighted data to my app.. Is this possible? – Saqib Vohra Apr 29 '13 at 20:49
  • I suspect you're out of luck. IIRC, the Cut/Copy/Paste menus are manually generated by every application that has context menus. There's no central place where you can affect the creation of those menus. – Edward Falk May 01 '13 at 22:00

2 Answers2

5

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

Roberto Tyley
  • 24,513
  • 11
  • 72
  • 101
  • Thanks alot. i think i have also messaged you on your gmail :p I also get another idea of doing that. with my service running, when ever user select and copy text, clipboard changes. on clipboard change my service will respond to it :) anyways thanks :D – Saqib Vohra May 02 '13 at 21:47
3

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

First, you have to add an intent filter in 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 that you want to launch when user selects a 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
}

Source

Ye Min Htut
  • 2,904
  • 15
  • 28