Send a text message from my android application to Whatsapp to specific Contact

大憨熊 提交于 2019-12-10 22:42:49

问题


I'm trying to send a text message from my android application to Whatsapp to specific Contact. when I'm using below codes, I am succeed either to send message and have to pickup contact manually, or If Specific number chat window opens, but message is blank. So is it possible to do both with one intent ? Here is my code:

  1. I can share message to WhatsApp, but contact i have to choose manually:

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("text/plain");        
    i.setPackage("com.whatsapp");       
    i.putExtra(Intent.EXTRA_TEXT, "Hello World");       
    try {           
        activity.startActivity(i);      
    } catch (Exception e) {             
        e.printStackTrace();        
    }
    
  2. Specific number in wats app window opens, but message is blank:

    Uri uri = Uri.parse("smsto:" + number);          
    Intent i = new Intent(Intent.ACTION_SENDTO, uri);
    i.putExtra("sms_body", "smsText");       
    i.setPackage("com.whatsapp");        
    activity.startActivity(i);
    

回答1:


Below code will helps you :

 String strMessageToShare=YourEditText.getText().tostring();
 final Intent myIntent = new Intent(
                    android.content.Intent.ACTION_SEND_MULTIPLE);
 myIntent.setType("text/plain");
 myIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                    new String[]{strMessageToShare});
 YourActivity.this.startActivityForResult(Intent
                    .createChooser(myIntent, "Sharing message..."), 1);



回答2:


This is what works for me.

The parameter 'body' gets not read by the whatsapp app, use 'Intent.EXTRA_TEXT' instead.

By setting the 'phoneNumber' you specify the contact to open in whatsapp.

    Intent sendIntent = new Intent(Intent.ACTION_SENDTO, 
           Uri.parse("smsto:" + "" + phoneNumber + "?body=" + encodedMessage));
    sendIntent.putExtra(Intent.EXTRA_TEXT, message);
    sendIntent.setPackage("com.whatsapp");
    startActivity(sendIntent);


来源:https://stackoverflow.com/questions/35647731/send-a-text-message-from-my-android-application-to-whatsapp-to-specific-contact

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!