通过Android中的WhatsApp向特定联系人发送消息

时间:2017-07-27 07:05:21

标签: android text share whatsapp

我尝试了很多解决方案,但在我的应用中向特定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);

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

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

enter image description here

2 个答案:

答案 0 :(得分:3)

我想你想要这样。

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

希望这会有所帮助。

答案 1 :(得分:0)

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

您错过了从“对话”类中检索旧对话的设置组件部分。