如何将Whatsapp消息发送到新号码

时间:2015-08-07 08:25:51

标签: java android whatsapp android-developer-api

我' d喜欢通过单击一个按钮来发送一个whatsapp消息,该按钮来自Android Activity(来自服务器的数据)。 我必须发送新邮件的号码不是我手机上的现有联系人。 我知道如何从我的应用程序打开Whatsapp应用程序。 以下代码涉及从适配器打开whatsapp:

Intent sendIntent = new Intent();
sendIntent.setPackage("com.whatsapp");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

此代码打开Whatsapp,但我不知道如何传递我必须发送消息的号码

1 个答案:

答案 0 :(得分:0)

试试这个

public void onClickWhatsApp(View view) {

    PackageManager pm=getPackageManager();
    try {

        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
        String text = "YOUR TEXT HERE";

        PackageInfo info=pm.getPackageInfo("com.whatsapp",     PackageManager.GET_META_DATA);
        //Check if package exists or not. If not then code 
        //in catch block will be called
        waIntent.setPackage("com.whatsapp");

        waIntent.putExtra(Intent.EXTRA_TEXT, text);
        startActivity(Intent.createChooser(waIntent, "Share with"));

   } catch (NameNotFoundException e) {
        Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                .show();
   }  

}