想要打开whatsapp与Android应用程序中未保存的联系人号码聊天

时间:2016-07-09 02:08:11

标签: 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

1 个答案:

答案 0 :(得分:2)

方法1-使用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);
}