2016-07-29 87 views
5

我想對特定用戶進行WhatsApp調用。我想這和它不工作:android-make 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); 
+0

能否請您更新相關錯誤日誌/調試信息您的問題。 –

+0

你把相關的'permissions'加到'manifest'上了嗎? – CodeWalker

+0

@LancePreston是的 – israelbenh

回答

11

簡單的解決方案是,查詢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

現在保存在數據庫或其他地方只有那些_ids其MIME類型是vnd.android.cursor.item/vnd.com.whatsapp.voip.call

然後你初始TE 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); 
+0

男人的工作!謝謝!!!!!!! – israelbenh

+0

這真的很有幫助,很棒。非常感謝你。 –

+0

我的兩美分,請檢查MIME類型的平穩運行。 – Skynet

7

爲了使下面的mime-串WhatsApp的視頻通話使用

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

爲了使下面的mime-串WhatsApp的語音通話使用

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

使用以下代碼

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(type)) { 
        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); 
       } 
      } 
     } 
2

如果我們傳遞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; 

} 
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; 
     } 
    } 
+0

下次請檢查您的格式。您的帖子質量很差,並且存在縮進問題。 –

1
// 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; 
     } 
    } 
+0

傢伙按照此代碼「檢查號碼有whatsApp」,對於「WhatsApp Call」和「WhatsApp視頻通話」試試這個100%,您將得到確切的結果 –

+0

爲whtsApp音頻和視頻通話遵循代碼 –