相关疑难解决方法(0)

从其他应用程序接收数据 - android

我阅读了有关接收简单数据的文档。我想接收一个 URL,即来自其他应用程序的文本/纯文本。

所以,我只声明了这个意图过滤器:

<intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
Run Code Online (Sandbox Code Playgroud)

在我的MainActivity.class 中:

void onCreate (Bundle savedInstanceState) {
    ...
 // Get intent, action and MIME type

        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();

        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                handleSendText(intent); // Handle text being sent
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

我将收到的文本处理为:

void handleSendText(Intent intent) {
        String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
        if (sharedText != null) {
            // Update UI …
Run Code Online (Sandbox Code Playgroud)

java android intentfilter android-intent android-implicit-intent

5
推荐指数
1
解决办法
1万
查看次数