android - 通过 WhatsApp 发送消息

标签 android android-intent social-networking sharing whatsapp

因为我发现了一些较旧的帖子,告诉我 whatsapp 不支持这个,我想知道是否有什么改变,以及是否有办法打开一个 whatsapp“聊天”,其中包含我通过 Intent ?

最佳答案

更新 请引用https://faq.whatsapp.com/en/android/26000030/?category=5245251

WhatsApp's Click to Chat feature allows you to begin a chat with someone without having their phone number saved in your phone's address book. As long as you know this person’s phone number, you can create a link that will allow you to start a chat with them.

Use: https://wa.me/15551234567

Don't use: https://wa.me/+001-(555)1234567

Example: https://wa.me/15551234567?text=I'm%20interested%20in%20your%20car%20for%20sale

原始答案 这是解决方案

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

}

另见 http://www.whatsapp.com/faq/en/android/28000012

关于android - 通过 WhatsApp 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34172509/

相关文章:

java - 设备上的 NoClassDefFoundError : com. google.android.gms.common.GooglePlayServicesUtil

android - 扩展 AppCompatActivity 会使应用崩溃

java - 如何在抽屉布局(带有抽屉导航菜单)中使用Android导航组件(导航图)?

java - 在 Android 应用程序中查看 MS Office 文件

julia - 在 Julia 中定义非常稀疏的网络矩阵的最有效方法是什么?

java - 如何保持小部件的JSONArray?

android - 当应用程序处于停止状态时接收 Android GCM 通知

java - 不幸的是我的应用程序已停止工作

r - 食物网中的营养位置/高度(沿着网络中的路径)

mysql - 适用于大量记录的数据库架构(例如社交网络中的消息)