开发者

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

开发者 https://www.devze.com 2023-03-04 08:05 出处:网络
I\'m trying to develop an Android application that provides an extra option when pasting data anywhere.

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 th开发者_JAVA技巧e user the option to paste the data on the clipboard after processing it, as an alternative to pasting it in the normal way.


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


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消