How do I open conversation of a particular contact in Whatsapp [duplicate]

拟墨画扇 提交于 2020-01-06 20:17:49

问题


I want to send a message using whatsapp only to a particular contact without the user having to select it from his contact list. I am using the below code to fire an intent

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

But this opens the whatsapp contact screen from which the user has to select the contact name.


回答1:


Try the below code.

try {
   String num= "+918888888888"; // contains spaces.
            num= num.replace("+", "").replace(" ", "");

            Intent sendIntent = new Intent("android.intent.action.MAIN");
            sendIntent.putExtra("jid", num+ "@s.whatsapp.net");
            sendIntent.putExtra(Intent.EXTRA_TEXT, "Test");
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.setPackage("com.whatsapp");
            sendIntent.setType("text/plain");
            startActivity(sendIntent);
} catch (Exception e) {
   app.showSnackBar(parentView, "Whatsapp have not been installed.");
}


来源:https://stackoverflow.com/questions/35284109/how-do-i-open-conversation-of-a-particular-contact-in-whatsapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!