android-make whatsapp call

2022-09-03 16:54:17

我想给特定用户打一个WhatsApp电话。我试过这个,它不起作用:

Uri uri = Uri.parse("callto:" + phoneNUmber);
Intent i = new Intent(Intent.ACTION_CALL, uri);
i.setPackage("com.whatsapp");
startActivity(i);

我知道如何创建一个WhatsApp消息,代码是相似的,它的工作原理:

Uri uri = Uri.parse("smsto:" + phoneNUmber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.setPackage("com.whatsapp");
startActivity(i);

答案 1

简单的解决方案是,查询联系人合同.数据_id和MIME类型。

ContentResolver resolver = context.getContentResolver();  
cursor = resolver.query(
            ContactsContract.Data.CONTENT_URI,
            null, null, null,
            ContactsContract.Contacts.DISPLAY_NAME);

//Now read data from cursor like 

while (cursor.moveToNext()) {
      long _id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Data._ID));
      String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
      String mimeType = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));

      Log.d("Data", _id+ " "+ displayName + " " + mimeType );

}

输出将如下所示

12561 Snow vnd.android.cursor.item/vnd.com.whatsapp.profile

12562 Snow vnd.android.cursor.item/vnd.com.whatsapp.voip.call

现在保存在 DB 中或其他地方,仅保存 MIME 类型为 _Idsvnd.android.cursor.item/vnd.com.whatsapp.voip.call

然后你以这种方式与这些联系人发起Whatsapp呼叫

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);

// the _ids you save goes here at the end of /data/12562     
intent.setDataAndType(Uri.parse("content://com.android.contacts/data/_id"),
    "vnd.android.cursor.item/vnd.com.whatsapp.voip.call");
intent.setPackage("com.whatsapp");
 
startActivity(intent);

答案 2

要使WhatsApp视频通话使用以下哑剧字符串:

String mimeString = "vnd.android.cursor.item/vnd.com.whatsapp.video.call";

要进行WhatsApp语音通话,请使用以下哑剧字符串:

 String mimeString = "vnd.android.cursor.item/vnd.com.whatsapp.voip.call";

使用以下代码:

 String displayName = null;
 String name="ABC" // here you can give static name.
 Long _id;
 ContentResolver resolver = getApplicationContext().getContentResolver();
 cursor = resolver.query(ContactsContract.Data.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME);
 while (cursor.moveToNext()) {
    _id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Data._ID));
    displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
    String mimeType = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
    if (displayName.equals(name)) {
        if (mimeType.equals(mimeString)) {
            String data = "content://com.android.contacts/data/" + _id;
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_VIEW);
            sendIntent.setDataAndType(Uri.parse(data), mimeString);
            sendIntent.setPackage("com.whatsapp");
            startActivity(sendIntent);
        }
    }
}

推荐