问题
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:
call to to
ClipboardManager.addPrimaryClipChangedListener()
and sign up as a listener when a user copies text. can be found in the DocumentationAdd 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