Nik*_*hil 5 java android intentfilter android-intent android-implicit-intent
我阅读了有关接收简单数据的文档。我想接收一个 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 to reflect text being shared
textBox.setText(sharedText);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,文档中写道,我应该小心处理其他类型的 MIME 类型。
1)但是,由于我只注册了纯文本/文本,我是否需要更多类型处理代码?
2)此外,引用文档:
请记住,如果此活动可以从系统的其他部分(例如启动器)启动,那么您在检查意图时需要考虑到这一点。
MainActivity.java 也是由 LAUNCHER 启动的。我该如何处理?
3)一旦用户从对话框中选择我的应用程序,在所有情况下它都会打开该应用程序吗?我不需要打开我的应用程序。我可以绕过这个吗?
编辑:我不需要打开我的应用程序的用户界面。但我想收到短信。
1.是的,您需要mimeType
为所有想要共享的文件类型添加 s。
2.我认为问题可能是一开始Activity
也会有
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />.
Run Code Online (Sandbox Code Playgroud)
在其明显声明中。所以当你打电话时
intent.getAction()
Run Code Online (Sandbox Code Playgroud)
返回哪个操作?ACTION_SEND
或者MAIN
?我相信这就是他们正在谈论的问题。
3.如果您不希望您的应用程序显示在共享应用程序列表中,那么为什么要添加该操作
<action android:name="android.intent.action.SEND" />
Run Code Online (Sandbox Code Playgroud)
Activity
首先在清单中对此?因为,向意图过滤器添加操作的目的恰恰是可以通过发送隐式意图从另一个应用程序启动Activity
或Service
或。如果您不希望这种情况发生,那么您打算如何“共享文本”?BroadcastReceiver