覆盖 Android 共享意图的标题



Android 似乎会自动将共享意图的标题设置为应用程序的名称。我想覆盖它以使其更具描述性(例如,标题可以是"使用我的应用程序预览",而不是"我的应用程序"。

如何在Android的共享对话框弹出框中更改标题?

我已将以下代码添加到我的AndroidManifest.xml:

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

关键是通过将意图作为参数传递给Intent.createChooser(intent: Intent)来开始您的意图作为活动

这是 Kotlin 中的一个例子。我们都喜欢 Kotlin,不是吗?:)

Intent(Intent.ACTION_SEND).apply {
putExtra(Intent.EXTRA_TEXT, "Some Text")
type = "text/plain"
startActivity(Intent.createChooser(this, "My Title"))
}

<intent-filter>支持属性android:iconandroid:label。您可以使用它们来更改系统共享 UI 中显示的图标和标签。
请注意,(至少在 Android 10 上(应用程序名称仍会显示。因此,您可能只想使用"预览"作为标签,而不是"使用我的应用程序预览"。

例:

<intent-filter 
android:icon="@drawable/share_icon"
android:label="@string/share_label">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>

见 https://developer.android.com/guide/topics/manifest/intent-filter-element

标题必须与共享意图一起出现。因此,如果您的应用程序是可以显示预览的应用程序之一,则无法显示用于选择应用程序的标题(它必须来自调用应用程序(。

另一方面,如果您在应用程序本身中使用共享意图,则可以尝试以下操作:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "What ever text you cant to share");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, "Preview with:")); <---- here

检查创建选择器的说明

最新更新