Android的:如何听longclick在其它应用程序的文本区域的事件?

问题描述:

我想开发一个Android应用程序任何地方粘贴数据时,提供了一个额外的选项。

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

我知道如何从剪贴板中捕获数据。我只需要知道如何倾听longclick事件在其他应用程序,如浏览器,Facebook,微博...等任何文本区域,这样我的应用程序将被触发,使用户的数据粘贴处理后剪贴板中的选项它,作为替代粘贴它以正常的方式

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.

您需要一个意图过滤器添加到活动中的问题,像这样:

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