Monthly Archives: February 2015

Listen to content uri changes

If you have an URI that you want to listen to but no broadcast is available from the content uri (No Broadcast Receiver available), then you can use ContentObserver class for this purpose.

Class:

public class myContentOberser extends ContentObserver{</code>

private String Uri contentUri = "";
private ContentResolver resolver = null;

public myContentObserver(Handler handler){
super(handler);
}

@Override
public void onChange(boolean selfChange){
// Perform what you want to do here.
}

public void startObserve(Context context, Uri uri){
if (resolver == null){
resolver = context.getContentResolver();
}

// Register the required uri to the system
resolver.registerContentObserver(uri, false, this);
}

public void stopObserver(){
if (resolver != null){
resolver.unregisterContentObserver(this);
}
}
}

Advertisements