Reputation: 11999
I have implemented a sync adapter which sends all my contacts over the server. But now I am faced with the issue that how the system can respond if it contact list has been updated.
I used the content observer to find out but it seems to be limited to activity lifecycle.
This is my code:
class MyObserver extends ContentObserver {
public MyObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
this.onChange(selfChange, null);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
Log.e("change ", "contact");
}
}
To register it:
context.getContentResolver().registerContentObserver(
Contacts.CONTENT_URI, false, new MyObserver(new Handler()));
Isn't there any broadcast to notify about it.
Or I have to make a service for it.
Upvotes: 0
Views: 982
Reputation: 2757
For that you have to run a service as follows
public class ContactService extends Service {
MyObserver observer;
@Override
public void onDestroy() {
// TODO Auto-generated method stub
getContentResolver().unregisterContentObserver(observer);
super.onDestroy();
}
@Override
public void onCreate() {
System.out.println("service created");
observer=new MyObserver(new Handler());
// TODO Auto-generated method stub
getContentResolver().registerContentObserver(Contacts.CONTENT_URI, false, observer);
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
Then you will notify when ever contact data is changed.
Upvotes: 2