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

Posted on February 4, 2015, in Uncategorized and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s