首页 文章

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

提问于
浏览
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,但我不知道如何传递它我必须发送消息的号码

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

相关问题