1

我想通过单击一个按钮来发送一条 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,但我不知道如何将我必须发送消息的号码传递给它

4

1 回答 1

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

}
于 2015-08-07T08:39:51.793 回答