在短信、WhatsApp或Telegram中选择发送消息



在我的Android应用程序中的联系人列表中,有一个启动WhatsApp的选项,实现如下:

// Country code is required
final String phoneNumber = "+15555555555";
final String packageName = "com.whatsapp";
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
if (null == intent) {
    // Launch Google Play at WhatsApp homepage
    intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("market://details?id=" + MESSAGE_PACKAGE_NAME));
    startActivity(intent);
    return;
}
intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + phoneNumber));
intent.setPackage(packageName);
startActivity(intent);

但这只允许我通过WhatsApp发送消息。

删除程序包名称并将意图类型设置为

intent.setType("vnd.android-dir/mms-sms");

启动SMS应用程序。

我们如何在安卓设备上安装的所有使用电话号码和标识符的应用程序中进行选择(Hangouts、SMS、Skype、Line、Telegram、Viber、WhatsApp等(?

幸运的是,Android Intent.createChooser足够聪明,能够找出将电话号码理解为标识符的应用程序:-(

// Country code is required
String phoneNumber = "+15555555555";
Uri uri = Uri.parse("smsto:" + phoneNumber);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(Intent.createChooser(intent, "Send message"));

最新更新