【问题标题】:Send message to a specific number in WhatsApp向 WhatsApp 中的特定号码发送消息
【发布时间】:2015-01-22 16:22:34
【问题描述】:

我正在使用此代码向 WhatsApp 中的特定号码发送消息:

    public void sendMessage() {
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            String whatsappid = "962795195996@s.whatsapp.net";

            Cursor c = getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI,
                    new String[] { ContactsContract.Contacts.Data._ID },
                    ContactsContract.Data.DATA1 + "=?",
                    new String[] { whatsappid }, null);
            c.moveToFirst();

            Intent whatsapp = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("content://com.android.contacts/data/"
                            + c.getString(0)));

            c.close();

            whatsapp.putExtra(Intent.EXTRA_TEXT, "Test...");

            whatsapp.setPackage("com.whatsapp");

            try {
                startActivity(whatsapp);
            } catch (android.content.ActivityNotFoundException ex) {
                ex.printStackTrace();
            }
        }
    });
}

运行代码时,WhatsApp 会在带有该号码的消息历史记录中打开,但文本消息未填写在输入框中。怎么解决?

【问题讨论】:

    标签: android whatsapp


    【解决方案1】:

    更改意图.ACTION_VIEW 到 Intent.ACTION_SEND

    并添加 whatsapp.setType("text/plain");

    【讨论】:

    • 我像你说的那样修改了我的代码,但现在不是打开对话页面,而是打开联系人列表以选择要向其发送消息的联系人,然后发送。有什么想法吗?
    猜你喜欢
    • 2013-09-04
    • 2015-07-08
    • 2017-09-17
    • 2015-05-26
    • 1970-01-01
    • 2013-09-24
    • 2018-06-28
    • 1970-01-01
    相关资源
    最近更新 更多