2012-05-02 82 views
1

我想知道它是否可能獲取SIM卡或電話簿中存在的聯繫人。現在,我正在使用以下代碼來獲取聯繫人,並且它甚至可以獲取所有聯繫人,甚至是我的Gmail和Facebook聯繫人。獲取本地電話簿聯繫人從SIM卡只有android

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

        PhoneBookUserEntity user = new PhoneBookUserEntity(); 
        // Pick out the ID, and the Display name of the 
        // contact from the current row of the cursor 
        user.setId(cursor.getString(cursor.getColumnIndex(BaseColumns._ID))); 
        user.setPhoneBookName(cursor.getString(cursor.getColumnIndex(
          ContactsContract.Contacts.DISPLAY_NAME))); 
        if(user.getphonebookname().length() > 4) 
        username = user.getphonebookname();//.substring(0,4); 
        else 
         username = user.getphonebookname();//.substring(0,1); 
        String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
        // if (Boolean.parseBoolean(hasPhone)) { 
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ user.getId(), null, null); 
        while (phones.moveToNext()) { 
         user.sePhoneNumber(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));     
        } 
        phones.close(); 
        //} 
        // user.sePhoneNumber(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); 

        Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + user.getId(), null, null); 
        while (emails.moveToNext()) { 
         // This would allow you get several email addresses 
         user.setEmailAddress(emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA))); 
        } 
        emails.close(); 
        user.setImageURI(getPhotoUri(user.getId())); 
        SearchContactsActivity.this.runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          // TODO Auto-generated method stub 
          _progressDialog.setMessage("Copying over your local phone book. Retrieving contact information for \n"+ username.toUpperCase()); 
         } 
        }); 
        arraylist.add(user); 
       } 

      } 
      cursor.close(); 

回答

6

對於SIM卡聯繫人不僅可以使用下面的代碼

private void allSIMContact() 
    { 
     try 
     { 
      String ClsSimPhonename = null; 
      String ClsSimphoneNo = null; 

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

      Log.i("PhoneContact", "total: "+cursorSim.getCount()); 

      while (cursorSim.moveToNext()) 
      {  
       ClsSimPhonename =cursorSim.getString(cursorSim.getColumnIndex("name")); 
       ClsSimphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number")); 
       ClsSimphoneNo.replaceAll("\\D",""); 
       ClsSimphoneNo.replaceAll("&", ""); 
       ClsSimPhonename=ClsSimPhonename.replace("|",""); 

       Log.i("PhoneContact", "name: "+ClsSimPhonename+" phone: "+ClsSimphoneNo); 
      }   
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 

    } 
0
Cursor cursor = getContacts(); 
    getBundleValues2(); 

    String displayName = ""; 
    while (cursor.moveToNext()) { 
     //taking id name and phone number from contacts using content provider 
     String displayid = cursor.getString(cursor 
       .getColumnIndex(ContactsContract.Contacts._ID)); 

     displayName = cursor.getString(cursor 
       .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

     String displayphnno = ""; 

     ContentResolver cr = getContentResolver(); 

     if (Integer.parseInt(cursor.getString(cursor 
       .getColumnIndex(ContactsContract.Data.HAS_PHONE_NUMBER))) > 0) { 

      Cursor pCur = cr.query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
          + " = ?", new String[] { displayid }, null); 
      while (pCur.moveToNext()) { 
       displayphnno = pCur 
         .getString(pCur 
           .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) 
         + "\n"; 

       ContactProviderViewGroup v6; 
       //using view group appending it to an activity 
       if (colorFlag) { 
        v6 = new ContactProviderViewGroup(this, displayName, 
          displayphnno, passedid, colorFlag); 
        colorFlag = false; 
       } else { 
        v6 = new ContactProviderViewGroup(this, displayName, 
          displayphnno, passedid, colorFlag); 
        colorFlag=true; 
       } 
       LL1.addView(v6); 
      } 
      pCur.close(); 
     } 

    } 

} 

private void getBundleValues2() { 
    Intent i = getIntent(); 
    Bundle myBundle = i.getExtras(); 

    if (myBundle != null) { 
     passedid = myBundle.getInt("gid"); 
    } 
} 

private Cursor getContacts() { 
    // Run query 
    Uri uri = ContactsContract.Contacts.CONTENT_URI; 

    String[] projection = new String[] { ContactsContract.Contacts._ID, 
      ContactsContract.Contacts.DISPLAY_NAME, 
      ContactsContract.Contacts.HAS_PHONE_NUMBER }; 

    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" 
      + ("1") + "'"; 
    String[] selectionArgs = null; 
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME 
      + " COLLATE LOCALIZED ASC"; 

    return managedQuery(uri, projection, selection, selectionArgs, 
      sortOrder); 
} 
相關問題