2013-01-12 52 views

回答

1

欢迎您注册一个ContentObserverContactsContractContentProvider适当Uri摸透更改该Uri

+0

我可以知道哪些联系人是添加/编辑/删除吗? – Deqing

+0

@CommonsWare - 任何相同的例子? –

+0

@AtolOHolic:我没有,我也没有去找任何东西。 – CommonsWare

0
//initially all your contact is stored in database then use this service and regiter this service in manifest 
public class ContactSync_Service extends IntentService { 

public ContactSync_Service() { 
    super("intent service"); 

} 

@Override 
protected void onHandleIntent(Intent intent) { 

    MyContentObserver contentObserver = new MyContentObserver(); 
    this.getApplicationContext() 
      .getContentResolver() 
      .registerContentObserver(ContactsContract.Contacts.CONTENT_URI, 
        true, contentObserver); 
} 

@Override 
public void onCreate() { 
    // TODO Auto-generated method stub 
    super.onCreate(); 

} 

class MyContentObserver extends ContentObserver { 

    public MyContentObserver() { 
     super(null); 
    } 

//whenever a contact is add,modified,delete this method will get called 
    @Override 
    public void onChange(boolean selfChange) { 
     Log.e("changes", "changes"); 

     syncDatabase(); 
     super.onChange(selfChange); 
    } 

} 



//comparing database contact list with phone's contact list if there is contact is added or modified 
@TargetApi(18) 
@SuppressWarnings("unused") 
public void syncDatabase() { 

    ContactDatabase db = new ContactDatabase(this); 
//get all the data from database include contact and name 
//contact bean is a class containing setter and getter of getName() and getContact() 
    ArrayList<ContactBean> arrayListDataase = db.fetch(); 
    int size = arrayListDataase.size(); 
    Log.e("size of arraylist", size + ""); 
    /* 
    * ArrayList<String> arrayListContact=new ArrayList<String>(); 
    * 
    * 
    * for(int i=0;i<arrayListDataase.size();i++) { 
    * arrayListContact.add(arrayListDataase.get(i).getContact()); } 
    */ 

    Cursor phones = getContentResolver().query(
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, 
      null, null); 
    int i = 0; 

    if (phones.moveToFirst()) { 

     do { 

      boolean flag = false; 

      String phoneNumber = phones 
        .getString(phones 
          .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

      // Log.e("number", phoneNumber); 


      for (i = 0; i < size; i++) 
      { 

       //comparing database contact number with phone's contact list contact number 
       if (arrayListDataase.get(i).getContact().equalsIgnoreCase(phoneNumber)) 
       { 

        flag = true; 

        String nameFromContactList = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
        String nameFromDatabase=arrayListDataase.get(i).getName(); 


        //check if name is changed in contact list 
        if(!nameFromContactList.equalsIgnoreCase(nameFromDatabase)) 
        { 
         db.upDateNameWhenChangeInContactListOfPhone(nameFromContactList, phoneNumber); 

        } 


        break;  

       } 

      } 

      if (!flag) { 

       String image_uri=""; 
       Log.e("contact changed", phoneNumber); 
       String name = phones 
         .getString(phones 
           .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
       String email = phones 
         .getString(phones 
           .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
       String time_stamp = phones 
         .getString(phones 
           .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_LAST_UPDATED_TIMESTAMP)); 




       image_uri = phones 
          .getString(phones 
          .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)); 


       if((image_uri==null) || image_uri.matches("") ) 
       { 
        image_uri=""; 
       } 



       db.insert(name, phoneNumber, email, image_uri); 
       //break; 

      } 



     } while (phones.moveToNext()); 

     phones.close(); 

    } 

}} 
相关问题