在 android 中通过 WhatsApp 向特定联系人发送消息

Send message to particular contact through WhatsApp in android

我尝试了很多解决方案,但仍然无法向我的应用程序中的特定 whatsapp 联系人发送消息。这是我的代码:

  Intent sendIntent = new Intent("android.intent.action.MAIN");
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.setType("text/plain");
                sendIntent.putExtra(Intent.EXTRA_TEXT, "Hey");
                sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(smsNumber) + "@s.whatsapp.net"); 
                sendIntent.setPackage("com.whatsapp");
                mContext.startActivity(sendIntent);

它只是打开特定的联系人聊天 window 作为没有联系人姓名、个人资料照片和旧转换的新对话。

请帮我解决这个问题。另附上截图。

我想你想要这样。

private void openWhatsApp() {
        String text = message.getText().toString();
        if(whatsappInstalledOrNot("com.whatsapp")){
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("whatsapp://send?text="+text+"&phone="+mobileNumber.getText().toString()));
            startActivity(browserIntent);
        }else {
            Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                    .show();
        }
    }
private boolean whatsappInstalledOrNot(String uri) {
        PackageManager pm = getPackageManager();
        boolean app_installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            app_installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            app_installed = false;
        }
        return app_installed;
    }

希望这会有所帮助。

Intent sendIntent = new Intent("android.intent.action.MAIN");
        sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.Conversation"));
        sendIntent.putExtra(Intent.EXTRA_TEXT, "Hey");
        sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("91xxxx008686")+"@s.whatsapp.net");
//phone number without "+" prefix (countrycode & contact number without '+')

        startActivity(sendIntent);

您错过了从 'Conversation' class.

检索旧对话的设置组件部分