简体   繁体   中英

Android add item to global context menu

When you long press on something in Android, a context menu comes up. I want to add something to this context menu for all TextViews in the system.

For example, the system does this with Copy and Paste. I would want to add my own, and have it appear in every application.

Currently Android does not support this, you cannot override or hook functionality globally at the system level without the particular activity implementing an intent or activity that you expose. Even in the case of publishing an intent it wouldn't matter unless the application running is a consumer... and all the base system applications and obviously all applications prior to yours would not be without updating the app to consume.

Basically as it stands, this is not possible.

What exactly are you trying to accomplish with this global context menu, some sort of global "Search For" or "Send To" functionality that runs through your application?

Push the Share button (tree of three dots) within selection menu. Then you should select your own app. Does't work for input field content, unfortunately.

Add intent-filter in your file android-manifest.xml:

<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>

Get highlighted by user text in activity your app in method onCreate():

  @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
}

More information in the article on medium .

Thanks for user: YungBlade

Him answer on ru.stackoverflow.com

Hmm; I don't know if you can extend built in types like in eg. Ruby (my Java knowledge is not so big).

However you can derive your own MyTextView from TextView. Then substitute all your TextViews in layouts like this:

<TextView android:id="@+id/TextView01" />

to

<com.mydomain.mypackage.MyTextView android:id="@+id/TextView01" />

to automatically change type of all these fields.

Then you need to override all constructors (especially TextView(Context context, AttributeSet attrs) ).

After that all layout inflaters (automatic or manual) will create this type with no fuss.

Then you can create/register context menu callbacks as you wish.

This might be a little bit hacky, but you can trap the menu key at the application/activity level, check to see if your current active view is a text entry view, and then build and display your own custom popup menu.

It is possible, it's just a little tricky.

If you create/inflate a TextView, call setFocusable(false) on it, and then set it as the active view your Activity will still receive key events. You will have to forward all those events (trackball, touch, key) to your View tree by hand. (Inside your "onKeyDown" function you'd have to call the appropriate "onKeyDown" method for the top level View) You effectively have to trap the notion of 'focus' and dole it out to the correct view yourself.

While a little ugly, it may give you the desired results.

This would, however, only work in your own application. Previous answers are correct about it being impossible across the entire phone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM