簡體   English   中英

從我的Android應用程序發送文本消息到Whatsapp到特定聯系人

[英]Send a text message from my android application to Whatsapp to specific Contact

我正在嘗試從我的Android應用程序發送文本消息到Whatsapp到特定的聯系人。 當我使用下面的代碼時,我成功發送消息並且必須手動接收聯系人,或者如果打開特定號碼聊天窗口,但消息是空白的。 那么可以用一個意圖做兩件事嗎? 這是我的代碼:

  1. 我可以與WhatsApp分享信息,但聯系我必須手動選擇:

     Intent i = new Intent(Intent.ACTION_SEND); i.setType("text/plain"); i.setPackage("com.whatsapp"); i.putExtra(Intent.EXTRA_TEXT, "Hello World"); try { activity.startActivity(i); } catch (Exception e) { e.printStackTrace(); } 
  2. wats應用程序窗口中的特定數字打開,但消息為空:

     Uri uri = Uri.parse("smsto:" + number); Intent i = new Intent(Intent.ACTION_SENDTO, uri); i.putExtra("sms_body", "smsText"); i.setPackage("com.whatsapp"); activity.startActivity(i); 

下面的代碼將幫助您:

 String strMessageToShare=YourEditText.getText().tostring();
 final Intent myIntent = new Intent(
                    android.content.Intent.ACTION_SEND_MULTIPLE);
 myIntent.setType("text/plain");
 myIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                    new String[]{strMessageToShare});
 YourActivity.this.startActivityForResult(Intent
                    .createChooser(myIntent, "Sharing message..."), 1);

這對我有用。

參數'body'不會被whatsapp應用程序讀取,而是使用'Intent.EXTRA_TEXT'。

通過設置'phoneNumber',您可以指定要在whatsapp中打開的聯系人。

    Intent sendIntent = new Intent(Intent.ACTION_SENDTO, 
           Uri.parse("smsto:" + "" + phoneNumber + "?body=" + encodedMessage));
    sendIntent.putExtra(Intent.EXTRA_TEXT, message);
    sendIntent.setPackage("com.whatsapp");
    startActivity(sendIntent);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM