如何使用的&QUOT全新的Andr​​oid M个特征;文本选择"从您的应用程序之外提供?您的、如何使用、应用程序、文本

2023-09-05 03:32:33 作者:再见不如不见

背景

Android的M presents一种新的方式来处理选定的文本(链接的此处),甚至从你的应用程序之外。文本选择可处理为这样:

我知道这是可以从应用程序之外处理选定的文本,因为如果我去到Web浏览器(或任何其他地方,可以让文本选择),我可以看到,我可以使用API演示应用程序,以办理选定的文本。

问题

我看不到很多关于如何做到这一点的信息。

问题

我应该在code(和明显的),以便能够从应用程序之外处理所选文本添加? 是否有可能限制选择某些类型的文本?例如,提供给显示该应用仅当文本类型为有效的电话号码? 解决方案

首先,要澄清一个问题:在一个M模拟器,如果你选中的文字,你会看到新的浮动的行动模式。如果单击溢图标,你会看到API DEMOS显示:

点击,带来了从API演示应用程序的活动,显示了突出显示的文本:

更换的字段中的值,然后点击按钮作为替换为任何你强调了把你替换文本。

警告:下面的解释是检查API演示code和并购开发preVIEW文档。这是非常可能的,这将收到中号船为Realz的改变。情况因人而异,除非你用公制,在这种情况下YKMV。

有关活动,正在接收文本,支持 ACTION_PROCESS_TEXT 意图的行动。 EXTRA_PROCESS_TEXT 将举行一些文本,或 EXTRA_PROCESS_TEXT_READONLY 将举行,如果文本是只读的。该活动将通过 startActivityForResult调用()。结果意图可以有自己的 EXTRA_PROCESS_TEXT 值,这将是替换文本。

因此​​,具体的问题:

  

我应该在code(和明显的),以便能够从应用程序之外处理所选文本添加?

见上面。请注意,API演示活动( ProcessText )有这个<意向滤光器>

 <意向滤光器>
            <作用机器人:名称=android.intent.action.PROCESS_TEXT/>
            <类机器人:名称=android.intent.category.DEFAULT/>
            <数据机器人:MIMETYPE =text / plain的/>
        &所述; /意图滤光器>
 

本文档不讨论一个MIME类型。我没有运行任何实验,以确定MIME类型是必需的,而且我们还有什么可能会得到(的text / html 的东西,有跨度?)。

  

是否有可能限制选择某些类型的文本?例如,提供给显示该应用仅当文本类型为有效的电话号码?

这似乎不能够提供的文档。话虽这么说,这肯定是一个合理的想法(例如,在清单公布一个正则表达式,或多个正则表达式,通过元数据文本必须匹配)。

Background

Android M presents a new way to handle selected text (link here), even from outside of your app . Text selection can be handled as such:

I know it's possible to handle the selected text from outside the app, because if I go to the web browser (or any other place that allows text selection), I can see that I can use the "API demos" app to handle the selected text.

The problem

I can't see a lot of information about how to do it.

The question

What should be added in code (and manifest) to be able to handle the selected text from outside the app ? Is it possible to limit the selection to certain types of texts ? For example, offer to show the app only if the text type is a valid phone number ?

解决方案

First, to clarify the question: On an M emulator, if you highlight text, you will see the new floating action mode. If you click the overflow icon, you will see "API DEMOS" show up:

Clicking that brings up an activity from the API Demos app, showing the highlighted text:

Replacing the value in the field and clicking the button puts your replacement text in as a replacement for whatever you had highlighted.

WARNING: The following explanation is from inspecting the API Demos code and the M Developer Preview documentation. It is very possible that this will change before M ships for realz. YMMV, unless you use the metric system, in which case YKMV.

The activity in question, that is receiving the text, supports ACTION_PROCESS_TEXT as the Intent action. EXTRA_PROCESS_TEXT will hold some text, or EXTRA_PROCESS_TEXT_READONLY will hold it if the text is read-only. The activity will be invoked via startActivityForResult(). The result Intent can have its own EXTRA_PROCESS_TEXT value, which will be the replacement text.

So, to the specific questions:

What should be added in code (and manifest) to be able to handle the selected text from outside the app ?

See above. Note that the API Demos activity (ProcessText) has this <intent-filter>:

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

The documentation does not discuss a MIME type. I have not run any experiments to determine if the MIME type is required, and what else we might get (text/html for stuff that has spans?).

Is it possible to limit the selection to certain types of texts ? For example, offer to show the app only if the text type is a valid phone number ?

That wouldn't seem to be possible given the documentation. That being said, it's certainly a reasonable idea (e.g., advertise a regex, or multiple regexes, via metadata in the manifest that the text must match).