Android : How to listen to longclick events in any text area in other applications?

╄→гoц情女王★ 提交于 2019-12-07 09:06:02

问题


I'm trying to develop an Android application that provides an extra option when pasting data anywhere.

I know how to capture data from the clipboard. I just need to know how to listen to longclick events in any text area in other applications such as browsers,facebook,twitter...etc so that my application would be triggered giving the user the option to paste the data on the clipboard after processing it, as an alternative to pasting it in the normal way.


回答1:


We've come a long way since you asked this question but there are actually 2 ways to do that:

  1. call to to ClipboardManager.addPrimaryClipChangedListener() and sign up as a listener when a user copies text. can be found in the Documentation

  2. Add the ACTION_PROCESS_TEXT Intent Filter so the user can pick a custom action you created/start your app. More can be found in this Blog Post




回答2:


You need to add an intent filter to the activity in question, like so:

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

Then you just need to handle the data sent to you in the intent in your Activity

Uri data = getIntent().getData();
Bundle extras = getIntent().getExtras();
String messageText = "";
if (data != null) {
  messageText = data.toString();
} else if (extras != null) {
  messageText = extras.getString(Intent.EXTRA_TEXT);
}


来源:https://stackoverflow.com/questions/5944541/android-how-to-listen-to-longclick-events-in-any-text-area-in-other-applicatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!