android - 当我选择一些文本并单击共享时,它将显示我的应用程序
问题描述
我想构建一个android应用程序。它的方式是当我选择一些文本并单击共享时,它会显示我的应用程序。谁能帮助我我需要使用什么以及如何获得?
解决方案
您需要告诉 Android 您的应用可以处理文本。您可以通过在 Android 清单文件中定义意图过滤器来做到这一点。
例如:
<activity android:name=".ui.MyActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
在这里,您是在告诉系统,每当有人共享“文本/纯文本”类型的内容时,都将显示我的应用程序作为选项。
如果用户选择您的应用程序,您将在 ui.MyActivity 类中获取数据,如下所述:
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
}
} else {
// Handle other intents, such as being started from the home screen
}
...
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Update UI to reflect text being shared
}
}
推荐阅读
- sql-server - 如何从 SQL Server 中的列中分离分号分隔的值并将它们放在多行中?
- node.js - 已关闭或释放的渲染器窗口中的电子会话 webrequest 函数
- json - 如何将来自 2 个输入的数据发送到一个 json 服务器对象?
- python - 自定义模型的 Keras 多 GPU 模型失败
- amazon-web-services - AWS Cognito 用户池应用程序客户端中的设置意味着什么
- oop - 在我理解 DDD 的过程中遇到了一个令人困惑的情况
- sql-server - 将泰国名字解析为名字的最后一个
- sql - 加入 2 个表之间的多个字段。如何确定是哪个字段导致连接失败?
- powershell - 使用powershell更改构建变量不起作用
- r - 基于两个标准匹配和求和两个数据帧的变量的有效方法