want to open whatsapp to chat with a unsaved conta

2020-05-29 04:48发布

i want to open whatsapp chat box for some number which is not saved in user mobile.

I am using below code :

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);

But whatsapp is showing error : enter image description here

2条回答
爷、活的狠高调
2楼-- · 2020-05-29 05:29

Sorry for late reply. I suppose your problem must be solved by now. You can try following code to open conversation for not saved numbers:

private void openWhatsApp(String number) {
        try {
            number = number.replace(" ", "").replace("+", "");

            Intent sendIntent = new Intent("android.intent.action.MAIN");
            sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.Conversation"));
            sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(number)+"@s.whatsapp.net");
            context.startActivity(sendIntent);

        } catch(Exception e) {
            Log.e(TAG, "ERROR_OPEN_MESSANGER"+e.toString());
        }
    }
查看更多
闹够了就滚
3楼-- · 2020-05-29 05:42

Method 1 - Using android component name

 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);
}

Method 2 - Using 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);
}
查看更多
登录 后发表回答