android - 想打开 whatsapp 与 android 应用程序中未保存的联系电话聊天

标签 android android-intent whatsapp

我想为一些未保存在用户手机中的号码打开 whatsapp 聊天框。

我正在使用下面的代码:

Uri uri = Uri.parse("smsto:" + str_MobileNumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", "Hello");
i.setPackage("com.whatsapp");
mContext.startActivity(i);

但是 whatsapp 显示错误: enter image description here

最佳答案

方法一——使用android组件名

 public static void openWhatsAppConversation(Context context, String number, String message) {

    number = number.replace(" ", "").replace("+", "");

    Intent sendIntent = new Intent("android.intent.action.MAIN");

    sendIntent.setType("text/plain");
    sendIntent.putExtra(Intent.EXTRA_TEXT, message);
    sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
    sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(number) + "@s.whatsapp.net");

    context.startActivity(sendIntent);
}

方法 2 - 使用 whatsapp api uri

public static void openWhatsAppConversationUsingUri(Context context, String numberWithCountryCode, String message) {

    Uri uri = Uri.parse("https://api.whatsapp.com/send?phone=" + numberWithCountryCode + "&text=" + message);

    Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);

    context.startActivity(sendIntent);
}

关于android - 想打开 whatsapp 与 android 应用程序中未保存的联系电话聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38277585/

相关文章:

java - Android Studio 如何检查 ImageView 是否已经显示图像,如果是则禁用 Intent

java - 使用 twilio 沙箱在 whatsapp 上发送多条媒体消息

java - Android,通过我的应用程序打开 url

android - 重定向用户以打开互联网设置

java - Android Studio:Tab指示器处于 Activity 状态

Android自定义 Intent 过滤器未接收广播?

android - SQLite 数据库中的表情符号

java - 如何通过单击按钮打开 Whatsapp 组?

android - Fragment 到 Activity 通信的替代方式

android - 在 Toast .maketext() 方法中使用 getApplicationcontext() 和 this 有什么区别