我的解决方案可以在三星设备上正常工作,但在小米设备上 Whatsapp 会自动启动。我希望用户选择从哪个应用程序发送一些提供的文本。
当用户选择联系人时,此代码可以正确填充文本字段,但小米的问题仍然存在。
try {
if (isAppInstalled(context, "com.whatsapp") && isAppInstalled(context,"com.viber.voip")) {
// Create intents for sending SMS, WhatsApp, and Viber
val smsIntent = Intent(Intent.ACTION_SEND)
.setType("vnd.android-dir/mms-sms")
.setData(Uri.parse("smsto:"))
.putExtra("sms_body", text)
val whatsappIntent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, text)
type = "text/plain"
setPackage("com.whatsapp")
}
val viberIntent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, text)
type = "text/plain"
setPackage("com.viber.voip")
}
// Create a list of Intent objects
val intentList = listOf(smsIntent,whatsappIntent, viberIntent)
// Check if there are any apps that can handle the given intents
val filteredIntentList = intentList.filter { intent ->
try {
packageManager.queryIntentActivities(
intent,
PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong())
).isNotEmpty()
} catch (e: Exception) {
// Handle exceptions that may occur during querying activities
false
}
}.distinctBy { it.`package`}
if (filteredIntentList.isNotEmpty()) {
// Create a chooser intent
val chooserIntent =
Intent.createChooser(filteredIntentList.first(), "Choose a messaging app")
chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS,
filteredIntentList.subList(1, filteredIntentList.size).toTypedArray()
)
// Start the activity
context.startActivity(chooserIntent)
} else {
// Handle the case when no apps can handle the given intents
}
}
} catch (e: Exception) {
e.printStackTrace()
// Handle any exceptions that may occur during the process
}
我还将其添加到清单中:
<queries>
<intent>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="text/plain" />
</intent>
// <package android:name="com.whatsapp" />
<package android:name="com.viber.voip" />
<package android:name="com.android.mms" />
</queries>
我和我的团队遇到了同样的问题,但是小米设备由于其定制的 MIUI Rom 有时可能会表现不同。
try {
val text = "Your message here"
val smsIntent = Intent(Intent.ACTION_SENDTO)
.setData(Uri.parse("smsto:"))
.putExtra("sms_body", text)
val whatsappIntent = Intent(Intent.ACTION_SEND)
.putExtra(Intent.EXTRA_TEXT, text)
.type = "text/plain"
whatsappIntent.`package` = "com.whatsapp"
val viberIntent = Intent(Intent.ACTION_SEND)
.putExtra(Intent.EXTRA_TEXT, text)
.type = "text/plain"
viberIntent.`package` = "com.viber.voip"
val intentList = listOf(smsIntent, whatsappIntent, viberIntent)
// Filter out intents that can't be handled
val filteredIntentList = intentList.filter { intent ->
try {
packageManager.queryIntentActivities(
intent,
PackageManager.MATCH_DEFAULT_ONLY
).isNotEmpty()
} catch (e: Exception) {
// Handle exceptions that may occur during querying activities
false
}
}.distinctBy { it.`package` }
if (filteredIntentList.isNotEmpty()) {
// Create a chooser intent
val chooserIntent = Intent.createChooser(filteredIntentList.first(), "Choose a messaging app")
chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS,
filteredIntentList.subList(1, filteredIntentList.size).toTypedArray()
)
// Start the activity
startActivity(chooserIntent)
} else {
// Handle the case when no apps can handle the given intents
// Show a message to the user or take alternative action
}
} catch (e: Exception) {
e.printStackTrace()
// Handle any exceptions that may occur during the process
}
另请注意: 也许它无法解决您使用小米设备的问题,可能您需要阅读(制造商特定文档)了解如何处理隐式意图