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