Android 시스템 연락처 운영 전체 버전

10867 단어
인터넷상에서 여러 가지 버전의 연락처를 찾았지만 모두 뜻대로 되지 않아 개인이 수정하고 정리한 풀버전을 공유했다.모두가 혼자서 더듬는 시간을 줄이다.
연락처 핸드폰 내장 수령, 추가, 삭제, 수정,SIM카드 연락처 수령, 추가, 삭제, 수정을 포함한다.
public class ContactData
{
    String id;
    String name;
    String number;
    
    public void setId(String idValue){
        id = idValue;
    }
    public void setContactName(String contactName){
        name = contactName;
    }
    public void setNumber(String phoneNumber){
        number = phoneNumber;
    }
    
    public String getId(){
        return id;
    }
    public String getContactName(){
        return name;
    }
    public String getNumber(){
        return number;
    }
}
/**
 *        ,  ,  ,  ,     。 
 * */
public class ContactsAccessPublic
{
    final static String TAG = "ContactsAccess";
    final static String PhoneAccountName = "Phone";
    final static String SIMAccountName = "SIM";
    
    //        
    public static List<ContactData> getPhoneContacts(Context context, List<ContactData> list, boolean bSort)
    {
        if(list == null)
            list = new ArrayList<ContactData>();
        
    	Cursor cursor = context.getContentResolver().query(RawContacts.CONTENT_URI,
    	          null,
    	          RawContacts.ACCOUNT_NAME + "=?",
    	          new String[]{"Phone"}, null);
        while (cursor.moveToNext())
        {
            int indexId = cursor.getColumnIndex(ContactsContract.Contacts._ID);
        	String contactId = cursor.getString(indexId);
        	int indexDisplayName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            String name = cursor.getString(indexDisplayName);
            
            Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
            while (phones.moveToNext())
            {
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                ContactData data = new ContactData();
                data.id = contactId;
                data.name = name;
                data.number = phoneNumber;     
                list.add(data);
            }
            phones.close();
        }
        cursor.close();
        return list;
    }
            
    public static List<ContactData> getSIMContacts(Context context, List<ContactData> list, boolean bSort) {  
        if(list == null)
            list = new ArrayList<ContactData>();
     CharacterParser characterParser = CharacterParser.getInstance();   
     ContentResolver resolver = context.getContentResolver();  
     //   Sims      
     Uri uri = Uri.parse("content://icc/adn");  
     Cursor phoneCursor = resolver.query(uri, null, null, null,null);
         //"sort_key asc");  
     
     if (phoneCursor != null) {  
     while (phoneCursor.moveToNext()) {  
         for(int i=0; i<phoneCursor.getColumnCount(); i++){
             String columnName = phoneCursor.getColumnName(i);
             String value = phoneCursor.getString(i);
             Log.e("","i: "+columnName+" value: "+value);
         }
     //         
//     String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);  
         //Phone._ID,
//       Phone.DISPLAY_NAME, Phone.NUMBER, "number"
         //FIXME 2.2   4.0    。
         int numberIndex = phoneCursor.getColumnIndex(Phone.NUMBER);
         if(numberIndex == -1){
             numberIndex = phoneCursor.getColumnIndex("number"); // Android2.2
         }
      String phoneNumber = phoneCursor.getString(numberIndex);
     //                        
     if (TextUtils.isEmpty(phoneNumber)) {
         continue;  
     }
     //          
     int nameIndex = phoneCursor.getColumnIndex(Phone.DISPLAY_NAME);
     if(nameIndex == -1)
         nameIndex = phoneCursor.getColumnIndex("name");// Android2.2
     String contactName = phoneCursor.getString(nameIndex);//phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);  
  
     //Sim           
     ContactData data = new ContactData();
     data.setId(phoneCursor.getString(phoneCursor.getColumnIndex(Phone._ID))); //(phoneCursor.getString(PHONES_ID_INDEX));
       data.setContactName(contactName);
       data.setNumber(phoneNumber);

       list.add(data);
     }  
  
     phoneCursor.close();  
     }
     
     return list;
    } 
    
    public static long insertSIMContact(Context context, ContactData contact)
    {
        ContentValues values = new ContentValues();
        Uri uri = Uri.parse("content://icc/adn");
        values.clear();
        values.put("tag", contact.getContactName());
        values.put("number", contact.getNumber());
        ContentResolver resolver = context.getContentResolver();
//        Uri newSimContactUri = resolver.insert(uri, values); //Android4.0
        ///////////////////////////////////////////////////////
        //for android 2.2
        Uri newSimContactUri = null;
        try{
            newSimContactUri = resolver.insert(uri, values);
        }
        catch(Exception e){
            e.printStackTrace();
            
//            values.clear();
////            values.put("name", contact.getContactName());
//            values.put("number", contact.getNumber());
//            newSimContactUri = resolver.insert(uri, values);
        }
        
        
        if(newSimContactUri != null){
            long id = ContentUris.parseId(newSimContactUri);
            contact.setId(id+"");
            return id;
        }
        else 
            return -1;
    }
    
    public static boolean updateSIMContact(Context context,ContactData oldContact, ContactData newContact)
    {
                Uri uri = Uri.parse("content://icc/adn");   
                ContentValues values = new ContentValues();   
                values.put("tag", oldContact.getContactName());
                values.put("number", oldContact.getNumber());
                values.put("newTag", newContact.getContactName());   
                values.put("newNumber", newContact.getNumber());   
                int rc = context.getContentResolver().update(uri, values, null, null);
              if (rc > 0){
                  return true;
              }
              else{
                  return false;
              }
    }   
    
    public static boolean insertPhoneContact(Context context, ContactData contact){
        /**
         *    RawContacts.CONTENT_URI        ,          rawContactId
         *       data    ,        ,                 
         */
            Uri rcUri;
            ContentValues values = new ContentValues();
            
//            ContentResolver resolver = context.getContentResolver();
            //   RawContacts.CONTENT_URI        ,          rawContactId 
            values.put(RawContacts.ACCOUNT_NAME, PhoneAccountName);
            values.put(RawContacts.ACCOUNT_TYPE, "null");
            values.put(ContactsContract.Contacts.DISPLAY_NAME, contact.getContactName());
            Uri rawContactUri =context.getContentResolver().insert(RawContacts.CONTENT_URI, values);
            long rawContactId = ContentUris.parseId(rawContactUri);
            contact.setId(rawContactId+"");
            
            // data      
            values.clear();
            values.put(Data.RAW_CONTACT_ID,rawContactId); 
            values.put(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE);//    
            values.put(StructuredName.GIVEN_NAME,contact.getContactName());
            rcUri = context.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI,values);
            
            if(rcUri != null){
              // data      
                values.clear();
                values.put(Data.RAW_CONTACT_ID,rawContactId);
                values.put(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE);
                values.put(Phone.NUMBER,contact.getNumber());
                values.put(Phone.TYPE,Phone.TYPE_WORK);
                rcUri = context.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI,values);

            }
            
            return (rcUri != null);
    }
    
    public static boolean updatePhoneContact(Context context, ContactData contact){
      
            ContentValues values = new ContentValues();
            
            values.clear();
            values.put(ContactsContract.Contacts.DISPLAY_NAME, contact.getContactName());
            int rc1 = context.getContentResolver().update(RawContacts.CONTENT_URI,values,
                    ContactsContract.Contacts._ID + "=?", new String[]{contact.getId()});
            
            values.clear();
            values.put(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE);
            values.put(Phone.NUMBER,contact.getNumber());
            int rc2 = context.getContentResolver().update(android.provider.ContactsContract.Data.CONTENT_URI,values,
                    Data.RAW_CONTACT_ID + "=?", new String[]{contact.getId()+""});
            return (rc1>0 || rc2 >0) ? true : false;
    }

    /**  contactId       */
    public static int deletePhoneContact(Context context,String name, String contactId)
    {  
        ContentResolver resolver = context.getContentResolver();
        int rc1 = 0, rc2 = 0;
        //  data    
        String where = ContactsContract.Data.CONTACT_ID + " =?";
        String[] whereparams = new String[] { contactId };
        rc1 = resolver.delete(ContactsContract.Data.CONTENT_URI, where, whereparams);
        
        //  rawContact    
        where = ContactsContract.RawContacts.CONTACT_ID + " =?";
        whereparams = new String[] { contactId };
        rc2 = resolver.delete(ContactsContract.RawContacts.CONTENT_URI, where, whereparams);
        
        return (rc1>0 && rc2>0) ? (rc1+rc2) : 0;
    }  
    
    public static int deleteSIMContact(Context context,ContactData contact)throws Exception{  
          Log.e("","deleteSIMContact name:"+contact.getContactName()+" id:"+contact.getId());
          Uri uri = Uri.parse("content://icc/adn");  
          ContentResolver resolver = context.getContentResolver();  
          int rc = resolver.delete(uri, "tag="+contact.getContactName()+" AND number="+contact.getNumber(),null);
          Log.e("", "rc2 = "+rc);
          return rc;
  }   

여기에는 다음과 같은 다른 블로거들의 내용이 포함되어 있습니다.
http://blog.csdn.net/lksodit_yiyi/article/details/7887228

좋은 웹페이지 즐겨찾기