将短信从我的android应用程序发送到Whatsapp到特定联系人

问题描述:

我正在尝试从我的android应用程序向Whatsapp发送文本消息到特定的联系人.当我使用以下代码时,我可以成功发送消息并必须手动接听联系人,或者如果特定号码聊天"窗口打开,但消息为空白.那么有可能同时实现两种意图吗?这是我的代码:

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. 我可以将消息共享给WhatsApp,但与我联系时必须手动选择:

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

  • wats应用程序窗口中的特定数字打开,但消息为空白:

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

  • 这对我有用.

    whatsapp应用程序未读取参数'body',请改用'Intent.EXTRA_TEXT'.

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

    通过设置"phoneNumber",您可以指定要在whatsapp中打开的联系人.

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