2017-10-10 112 views
-1

联系我想从移动 所有联系人号码我使用下面的代码,但它给唯一的联系数中移动没有SIM卡的得到所有的Android移动

private void getContactList() { 
    ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 

    if ((cur != null ? cur.getCount() : 0) > 0) { 
     while (cur != null && cur.moveToNext()) { 
      String id = cur.getString(
        cur.getColumnIndex(ContactsContract.Contacts._ID)); 
      String name = cur.getString(cur.getColumnIndex(
        ContactsContract.Contacts.DISPLAY_NAME)); 

      if (cur.getInt(cur.getColumnIndex(
        ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) { 
       Cursor pCur = cr.query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", 
         new String[]{id}, null); 
       while (pCur.moveToNext()) { 
        String phoneNo = pCur.getString(pCur.getColumnIndex(
          ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        ContactsPlacer obj = new ContactsPlacer(); 
        obj.setContactname(name); 
        obj.setContactnumber(phoneNo); 
        names.add(obj); 

       } 
       pCur.close(); 
      } 
     } 
    } 
    if(cur!=null){ 
     cur.close(); 
    } 
} 

回答

0

下面是代码正从设备

private void getAllContacts() { 
    try { 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { 
      Toast.makeText(this, "Read Contact permission not enabled", Toast.LENGTH_SHORT).show(); 
     } 
     else { 
      contactVOList = new ArrayList(); 
      ContactVO contactVO; 

      ContentResolver contentResolver = getContentResolver(); 
      Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 
      if (cursor.getCount() > 0) { 
       while (cursor.moveToNext()) { 

        int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); 
        if (hasPhoneNumber > 0) { 
         String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
         String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

         contactVO = new ContactVO(pref.getUser_ID(),phoneNumberArrayList); 
         contactVO.setContactName(name); 

         Cursor phoneCursor = contentResolver.query(
           ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
           null, 
           ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", 
           new String[]{id}, 
           null); 
         if (phoneCursor.moveToNext()) { 
          String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
          phoneNumber = phoneNumber.replace(" ", ""); 
          phoneNumber = phoneNumber.trim(); 
          String number = "+91#1234*121#"; 
          number=phoneNumber.replaceAll("[\\D]", ""); 
          contactVO.setContactNumber(number); 
          phoneNumberArrayList.add(number); 
         } 

         phoneCursor.close(); 

         Cursor emailCursor = contentResolver.query(
           ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
           null, 
           ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
           new String[]{id}, null); 
         while (emailCursor.moveToNext()) { 
          String emailId = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
         } 
         emailCursor.close(); 
         contactVOList.add(contactVO); 
        } 
       } 
       cursor.close(); 
       if (contactVOList.size() > 0) { 
        final Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() { 
         @Override 
         public void run() { 
          CallAPI(); 
         } 
        }, 1000); 


       } 
      } 
     } 
    } catch (NumberFormatException e) { 
     e.printStackTrace(); 
    } 
} 

所有联系,并添加以下代码获取SIM卡联系人

Uri simUri = Uri.parse("content://icc/adn");Cursor cursorSim = this.getContentResolver().query(simUri, null, null,null, null); 

    while (cursorSim.moveToNext()) {   
     listName.add(cursorSim.getString(cursorSim.getColumnIndex("name"))); 
     listContactId.add(cursorSim.getString(cursorSim.getColumnIndex("_id")));  
     listMobileNo.add(cursorSim.getString(cursorSim.getColumnIndex("number"))); 
     } 
+0

它会以排序的方式给予来自手机和SIM卡的所有联系人 –