web-dev-qa-db-ja.com

android-whatsapp呼び出しを行う

特定のユーザーに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);
10
israelbenh

単純な解決策は、_idおよびMIMEタイプのQuery ContactContract.Dataです。

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

次に、DBまたは他の場所に、MIMEタイプがvnd.Android.cursor.item/vnd.com.whatsapp.voip.callの_Idのみを保存します

そして、あなたはこのようにそれらの連絡先との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);
20
Adnan Khan

WhatsAppビデオ通話を行うには、以下のmime-stringを使用します。

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);
        }
    }
}
11
Zezariya Nilesh

このコードは、番号に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
dileep krishnan

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;

}
2
Vikas
// 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;
        }
    }
1
dileep krishnan