我正在尝试通过Whatsapp向未保存的号码发送消息,我有两个whatsapps,但我正在获取选择器,但无法为其添加号码,它显示了我最近的联系人列表。
我已经尝试了Uri,PackageManager和很多东西,但是没有输出
num = "91" + edt_num.getText().toString();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, "Hi, This is me");
sendIntent.putExtra("jid", num + "@s.whatsapp.net");
sendIntent.setPackage("com.whatsapp");
if (sendIntent.resolveActivity(getPackageManager()) == null) {
Toast.makeText(MainActivity.this, "Error/n", Toast.LENGTH_SHORT).show();
return;
}
startActivity(sendIntent);
答案 0 :(得分:0)
使用此功能将消息发送到未保存的号码
必须在电话号码中使用国家/地区代码,例如91xxxxxxxxxx
public void SendWhatsappMessage(String phoneNumber,String message){
PackageManager pm = getPackageManager();
Intent send= new Intent(Intent.ACTION_VIEW);
try {
String url = "https://api.whatsapp.com/send?phone="+ phoneNumber +"&text=" + URLEncoder.encode(message, "UTF-8");
send.setPackage("com.whatsapp");
send.setData(Uri.parse(url));
if (send.resolveActivity(pm) != null) {
startActivity(send);
}
} catch (Exception e){
e.printStackTrace();
}
}