How to Listen for a change in Contacts contentprovider

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

How to Listen for a change in Contacts contentprovider

Postby liji » Wed Aug 17, 2011 12:10 pm

Hi, I am new to android...
I am trying to get notified in my application whenever a new contact is added via the native contact application.
In my application, I made a Service & in the Service onStart() i register for a ContentObserver..
public class ContactService extends Service{

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();

}

@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
}

@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Handler handler = new Handler();
ContactContentObserver cco = new ContactContentObserver(handler);
ContentResolver cr = getContentResolver();
cr.registerContentObserver( Contacts.People.CONTENT_URI, true, cco );
}
}

public class ContactContentObserver extends ContentObserver{


public ContactContentObserver(Handler handler) {
super(handler);
Log.v("", "In ContactContentObserver");
// TODO Auto-generated constructor stub
}

public void onChange(boolean selfChange) {
Log.v( "" , "ContactContentObserver.onChange( "+selfChange+")" );
}

@Override
public boolean deliverSelfNotifications() {
return true;
}

}
If my application tries to add a new contact, the ContactContentObserver's onChange() gets called.
But if i put my application to background(by pressing on the home icon in the emulator) & add a new contact through the native contact application, my service dont not get notified ...
How should i write my class so that it can listen to the update made by native application.
liji
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Aug 01, 2011 8:52 am

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 6 guests