want to open whatsapp to chat with a unsaved contact number in android app

后端 未结 2 2008
轻奢々
轻奢々 2021-02-06 16:40

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_Mobile         


        
相关标签:
2条回答
  • 2021-02-06 17:19

    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());
            }
        }
    
    0 讨论(0)
  • 2021-02-06 17:36

    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);
    }
    
    0 讨论(0)
提交回复
热议问题