【问题标题】:android-make whatsapp callandroid-make whatsapp 调用
【发布时间】:2016-07-29 09:38:41
【问题描述】:

我想向特定用户拨打 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);

【问题讨论】:

  • 您能否用相关的错误日志/调试信息更新您的问题。
  • 您是否将相关的permissions 添加到manifest 中?

标签: java android whatsapp


【解决方案1】:

简单的解决方案是,查询 ContactContract.Data 的 _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 雪 vnd.android.cursor.item/vnd.com.whatsapp.profile

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

现在只将那些 MIME 类型为 vnd.android.cursor.item/vnd.com.whatsapp.voip.call 的 _Id 保存在 DB 或其他地方

然后你以这种方式与这些联系人发起 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);

【讨论】:

  • 我的两分钱,请检查 MIME 类型是否正常运行。
  • 如何使用intent进行类似viop通话的whatsApp视频通话?
  • 如果我的通讯录中没有号码怎么办?
  • 是否可以在不保存联系人的情况下拨打whatsapp电话?
  • @adnan-khan 这个解决方案对我不起作用。甚至没有发生异常/错误。似乎 Intent 被忽略了。
【解决方案2】:

要进行 WhatsApp 视频通话,请使用以下 mime 字符串:

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

要进行 WhatsApp 语音通话,请使用以下 mime-string:

 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);
        }
    }
}

【讨论】:

  • 这个解决方案对我不起作用。甚至没有发生异常/错误。似乎 Intent 被忽略了。
【解决方案3】:

此代码用于检查号码是否有whatsapp并进行whatsapp音频和视频通话

首先检查号码是否有whatsapp ...如果你不知道

 if  rowContactId (return type of hasWhatsapp) is  not equal to '0'   then this number has whatsapp.

.

public String hasWhatsapp(  getContactIDFromNumber(795486179).toString(),MAinactivity.this ) 
{
        String rowContactId = null;
        boolean hasWhatsApp;

        String[] projection = new String[]{ContactsContract.RawContacts._ID};
        String selection = ContactsContract.Data.CONTACT_ID + " = ? AND account_type IN (?)";
        String[] selectionArgs = new String[]{contactID, "com.whatsapp"};
        Cursor cursor = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, selectionArgs, null);
        if (cursor != null) {
            hasWhatsApp = cursor.moveToNext();
            if (hasWhatsApp) {
                rowContactId = cursor.getString(0);
            }
            cursor.close();
        }
        return rowContactId;
    }


public static int getContactIDFromNumber( contactNumber,Context context)
    {
        contactNumber = Uri.encode(contactNumber);
        int phoneContactID = new Random().nextInt();
        Cursor contactLookupCursor = context.getContentResolver().query(Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,contactNumber),new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}, null, null, null);
        while(contactLookupCursor.moveToNext())
        {
            phoneContactID = contactLookupCursor.getInt(contactLookupCursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
        }
        contactLookupCursor.close();        

        return phoneContactID;
    }


//your number is support for whatsapp then come to here  to make whatsapp  call
// this is for whatsapp call
 wtsapp_call.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                String mimeString = "vnd.android.cursor.item/vnd.com.whatsapp.voip.call";


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

                //here you have to pass whatsApp contact  number  as  contact_number ..

               String name= getContactName( contact_number, MainActivity.this);
                int whatsappcall=getContactIdForWhatsAppCall(name,MainActivity.this);
                if (whatsappcall!=0) {
                    intent.setDataAndType(Uri.parse("content://com.android.contacts/data/" +whatsappcall),
                            "vnd.android.cursor.item/vnd.com.whatsapp.voip.call");
                    intent.setPackage("com.whatsapp");

                    startActivityForResult(intent, WHATSAPP_NUMMBER);
                }
            }
        });


//for whatsapp  video call
        wtsapp_video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);

                       //here you have to pass whatsApp contact  number  as  number..

                        String name= getContactName( number, MainActivity.this);
                int videocall=getContactIdForWhatsAppVideoCall(name,MainActivity.this);
                if (videocall!=0)
                {
                    intent.setDataAndType(Uri.parse("content://com.android.contacts/data/" +videocall),
                            "vnd.android.cursor.item/vnd.com.whatsapp.video.call");
                    intent.setPackage("com.whatsapp");
                    startActivity(intent);
                }

            }
        });

 public String getContactName(final String phoneNumber, Context context)
    {
        Uri uri=Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phoneNumber));

        String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME};

        String contactName="";
        Cursor cursor=context.getContentResolver().query(uri,projection,null,null,null);

        if (cursor != null) {
            if(cursor.moveToFirst()) {
                contactName=cursor.getString(0);
            }
            cursor.close();
        }

        return contactName;
    }


 public  int getContactIdForWhatsAppCall(String name,Context context)
    {

        cursor = getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                new String[]{ContactsContract.Data._ID},
                ContactsContract.Data.DISPLAY_NAME + "=? and "+ContactsContract.Data.MIMETYPE+ "=?",
                new String[] {name,"vnd.android.cursor.item/vnd.com.whatsapp.voip.call"},
                ContactsContract.Contacts.DISPLAY_NAME);

        if (cursor.getCount()>0)
        {
            cursor.moveToNext();
            int phoneContactID=  cursor.getInt(cursor.getColumnIndex(ContactsContract.Data._ID));
            System.out.println("9999999999999999          name  "+name+"      id    "+phoneContactID);
            return phoneContactID;
        }
        else
        {
            System.out.println("8888888888888888888          ");
            return 0;
        }
    }

    public  int getContactIdForWhatsAppVideoCall(String name,Context context)
    {
      Cursor  cursor = getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                new String[]{ContactsContract.Data._ID},
                ContactsContract.Data.DISPLAY_NAME + "=? and "+ContactsContract.Data.MIMETYPE+ "=?",
                new String[] {name,"vnd.android.cursor.item/vnd.com.whatsapp.video.call"},
                ContactsContract.Contacts.DISPLAY_NAME);

        if (cursor.getCount()>0)
        {
            cursor.moveToFirst();
            int phoneContactID=  cursor.getInt(cursor.getColumnIndex(ContactsContract.Data._ID));
            return phoneContactID;
        }
        else
        {
            System.out.println("8888888888888888888          ");
            return 0;
        }
    }

【讨论】:

  • 下次请检查您的格式。您的帖子质量非常低,并且存在缩进问题。
【解决方案4】:

如果我们传递 rawContactId,那么我们可以使用它来直接获取与 whatsapp 调用 URI 关联的 ID。

private void whatsAppCall(Context context, String rawContactId) {
        try {

            int id = whatsAppCallId(context, rawContactId);

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            String uriString = "content://com.android.contacts/data/" + id;
            intent.setDataAndType(Uri.parse(uriString), "vnd.android.cursor.item/vnd.com.whatsapp.voip.call");
            intent.setPackage("com.whatsapp");
            startActivity(intent);
        } catch (Exception e) {
            Log.e(TAG, "whatsAppCall Exception: " + e);
        }
    }



 private long whatsAppCallId(Context context, String rawContactId){ 
    ContentResolver resolver = context.getContentResolver();

            String selection = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.Data.RAW_CONTACT_ID + " = ? ";
            String[] selectionArgs = new String[] { "vnd.android.cursor.item/vnd.com.whatsapp.voip.call", rawContactId };


            Cursor cursor = resolver.query(
                    ContactsContract.Data.CONTENT_URI,
                    null, selection, selectionArgs,
                    ContactsContract.Contacts.DISPLAY_NAME);
            long _id=0;

            while (cursor.moveToNext()) {
                _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(TAG, "Data: " + _id+ " "+ displayName + " " + mimeType );
            }
return _id;

}

【讨论】:

    【解决方案5】:
    // first  check wether  number have whatsapp   or  not  ...if you dont know
    
      //if  rowContactId (return type of hasWhatsapp) is  not equal to '0'   then this number has whatsapp..
    
    public String hasWhatsapp(  getContactIDFromNumber(795486179).toString(),MAinactivity.this ) 
    {
            String rowContactId = null;
            boolean hasWhatsApp;
    
            String[] projection = new String[]{ContactsContract.RawContacts._ID};
            String selection = ContactsContract.Data.CONTACT_ID + " = ? AND account_type IN (?)";
            String[] selectionArgs = new String[]{contactID, "com.whatsapp"};
            Cursor cursor = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, selectionArgs, null);
            if (cursor != null) {
                hasWhatsApp = cursor.moveToNext();
                if (hasWhatsApp) {
                    rowContactId = cursor.getString(0);
                }
                cursor.close();
            }
            return rowContactId;
        }
    
    
    public static int getContactIDFromNumber( contactNumber,Context context)
        {
            contactNumber = Uri.encode(contactNumber);
            int phoneContactID = new Random().nextInt();
            Cursor contactLookupCursor = context.getContentResolver().query(Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,contactNumber),new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}, null, null, null);
            while(contactLookupCursor.moveToNext())
            {
                phoneContactID = contactLookupCursor.getInt(contactLookupCursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
            }
            contactLookupCursor.close();        
    
            return phoneContactID;
        }
    
    
    //your number is support for whatsapp then come to here  to make whatsapp  call
    // this is for whatsapp call
     wtsapp_call.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    String mimeString = "vnd.android.cursor.item/vnd.com.whatsapp.voip.call";
    
    
                             Intent intent = new Intent();
                             intent.setAction(Intent.ACTION_VIEW);
    
                    //here you have to pass whatsApp contact  number  as  contact_number ..
    
                   String name= getContactName( contact_number, MainActivity.this);
                    int whatsappcall=getContactIdForWhatsAppCall(name,MainActivity.this);
                    if (whatsappcall!=0) {
                        intent.setDataAndType(Uri.parse("content://com.android.contacts/data/" +whatsappcall),
                                "vnd.android.cursor.item/vnd.com.whatsapp.voip.call");
                        intent.setPackage("com.whatsapp");
    
                        startActivityForResult(intent, WHATSAPP_NUMMBER);
                    }
                }
            });
    
    
    //for whatsapp  video call
            wtsapp_video.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_VIEW);
    
                           //here you have to pass whatsApp contact  number  as  number..
    
                            String name= getContactName( number, MainActivity.this);
                    int videocall=getContactIdForWhatsAppVideoCall(name,MainActivity.this);
                    if (videocall!=0)
                    {
                        intent.setDataAndType(Uri.parse("content://com.android.contacts/data/" +videocall),
                                "vnd.android.cursor.item/vnd.com.whatsapp.video.call");
                        intent.setPackage("com.whatsapp");
                        startActivity(intent);
                    }
    
                }
            });
    
     public String getContactName(final String phoneNumber, Context context)
        {
            Uri uri=Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phoneNumber));
    
            String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME};
    
            String contactName="";
            Cursor cursor=context.getContentResolver().query(uri,projection,null,null,null);
    
            if (cursor != null) {
                if(cursor.moveToFirst()) {
                    contactName=cursor.getString(0);
                }
                cursor.close();
            }
    
            return contactName;
        }
    
    
     public  int getContactIdForWhatsAppCall(String name,Context context)
        {
    
            cursor = getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI,
                    new String[]{ContactsContract.Data._ID},
                    ContactsContract.Data.DISPLAY_NAME + "=? and "+ContactsContract.Data.MIMETYPE+ "=?",
                    new String[] {name,"vnd.android.cursor.item/vnd.com.whatsapp.voip.call"},
                    ContactsContract.Contacts.DISPLAY_NAME);
    
            if (cursor.getCount()>0)
            {
                cursor.moveToNext();
                int phoneContactID=  cursor.getInt(cursor.getColumnIndex(ContactsContract.Data._ID));
                System.out.println("9999999999999999          name  "+name+"      id    "+phoneContactID);
                return phoneContactID;
            }
            else
            {
                System.out.println("8888888888888888888          ");
                return 0;
            }
        }
    
        public  int getContactIdForWhatsAppVideoCall(String name,Context context)
        {
          Cursor  cursor = getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI,
                    new String[]{ContactsContract.Data._ID},
                    ContactsContract.Data.DISPLAY_NAME + "=? and "+ContactsContract.Data.MIMETYPE+ "=?",
                    new String[] {name,"vnd.android.cursor.item/vnd.com.whatsapp.video.call"},
                    ContactsContract.Contacts.DISPLAY_NAME);
    
            if (cursor.getCount()>0)
            {
                cursor.moveToFirst();
                int phoneContactID=  cursor.getInt(cursor.getColumnIndex(ContactsContract.Data._ID));
                return phoneContactID;
            }
            else
            {
                System.out.println("8888888888888888888          ");
                return 0;
            }
        }
    

    【讨论】:

    • 伙计们按照此代码“检查号码是否有whatsApp”,对于“WhatsApp 通话”和“WhatsApp 视频通话”试试这个 100% 你会得到准确的结果
    • 哥们,这太复杂了,你能拍个视频吗?
    【解决方案6】:

    @Adnan Khan 提供的答案适用于 Android 11,但我想提供他的答案的 Kotlin 版本:

    val resolver: ContentResolver = requireContext().contentResolver
    val cursor: Cursor? = resolver.query(
        ContactsContract.Data.CONTENT_URI,
            null, null, null,
        ContactsContract.Contacts.DISPLAY_NAME)
    
    while(cursor!!.moveToNext()) {
        val _id: Long = cursor.getLong(cursor.getColumnIndex(ContactsContract.Data._ID))
        val displayName: String = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME))
        val mimeType: String = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE))
    
        Log.i("Intents", "$_id $displayName $mimeType")
    

    现在,Kotlin 的意图是这样的: _id 是你从之前的路由得到的值

    val intent = Intent()
    intent.action = Intent.ACTION_VIEW
    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)
    

    最后但同样重要的是,将以下内容添加到 AndroidManifest.xml:

    <uses-permission
        android:name="android.permission.CALL_PHONE"
        androd:maxSdkVersion="30" />
    <uses-permission
        android:name="android.permission.READ_CONTACTS"
        androd:maxSdkVersion="30" />
    

    在此之后,您需要转到设置 -> 应用程序 -> 选择您的应用程序并将这两个权限标记为允许。

    应该有一种方式让应用在第一次运行时询问你,但那是另一个话题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      • 2010-09-06
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      相关资源
      最近更新 更多