Android Marshmallow 文本选择选项菜单操作

Android Marshmallow Text Selection Option Menu Actions

您好,我想添加一个全局文本选择侦听器,它显示任何选定文本的子菜单。 Android 6 允许使用新的文本选择侦听器进行此操作。

是否可以通过外部应用程序使用此功能,然后填充子菜单?

这个概念被称为 ACTION_PROCESS_TEXT 并且在 Android 6:

中可用

在您的清单中定义一个意图过滤器:

<activity android:name=".YourActivity" 
          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>

然后处理你的意图 activity:

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
  boolean readonly = getIntent()
  .getBooleanExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, false);
}

每个Activity只能定义一个动作。

Source

Example