0

I want to implement a BroadcastReceiver to listen if contact has been deleted/ updated. I read about ContantObserver but I have no idea how to run this in application background, like BroadcastReceiver.

How can I implement this? Is there any way to run ContantObserver in background of an application.

I followed this FAQ.

Community
  • 1
  • 1
Android Learner
  • 2,559
  • 6
  • 34
  • 43

2 Answers2

0

Implement a broadcaste Receiver and send Broadcaste in the onChange method of ContentObserver targetting this receiver.. see this for using custom intents and broadcastes..

ngesh
  • 13,398
  • 4
  • 44
  • 60
  • My need is to implement this functionality from background of an application. I mean my application is not on foreground and if contact has been changed, it must be listen by my application. I think ContentObserver needs to be run on foreground. Am I right? – Android Learner Apr 18 '12 at 13:30
  • @AndroidLearner .. thats why i suggested receiver which runs in background.. and Content Observer don't have to be in foreground.. – ngesh Apr 18 '12 at 13:34
0

Start a service and register the ContentObserver to this server

Here is a sample code I did which will listen to any changes in the Contacts.CONTENT_URI and notify the user

This is the service

public class ContactObserverService extends Service implements ContactObserver.OnUpdate {
private ContactObserver contactObserver ;

public ContactObserverService() {
}

@Override
public void onCreate(){
    super.onCreate();
    contactObserver = new ContactObserver(this);
    Toast.makeText(this,"Service Started",Toast.LENGTH_SHORT).show();
    this.getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI,false,contactObserver);
}

@Override
public void onDestroy() {
    if( contactObserver !=null  )
    {
        this.getContentResolver().unregisterContentObserver(contactObserver);
    }
    super.onDestroy();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // The service is starting, due to a call to startService()
    return START_STICKY;
}

@Override
public void onUpdate(Uri uri) {
    NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setContentTitle("Contact Update Notifier")
            .setContentText(uri.getQuery())
            .setSmallIcon(R.drawable.images);
    NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    manager.notify(1,builder.build());
}
}

The ContentObserver class is

public class ContactObserver extends ContentObserver {
private OnUpdate onUpdate;

public interface OnUpdate{
    void onUpdate(Uri uri);
}
public ContactObserver(OnUpdate onUpdate) {
    super(new Handler());
    this.onUpdate = onUpdate;
}

@Override
public void onChange(boolean selfChange) {
    this.onChange(selfChange, null);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
    // this is NOT UI thread, this is a BACKGROUND thread
    uri.getQueryParameterNames();
    onUpdate.onUpdate(uri);
}
}
ArK
  • 20,698
  • 67
  • 109
  • 136
Mohanakrrishna
  • 148
  • 3
  • 13