Android content observer realizes the function of automatically refreshing album (gallery) after screenshots

Keywords: Database

The author has done an album application some time ago. The basic functions include querying the media library, displaying pictures in reverse chronological order, zooming in, zooming in, sliding pictures, calling albums externally, etc. interested students can confide in me. Today, only the function of automatic refresh of screenshots of album page system is introduced

When it comes to observing data changes and refreshing the UI, we should think of the observer mode. As an album application, we only need to monitor the corresponding system media library changes. The system provides the ContentObserver class to help us realize the functions. See the code below, and the comments are very clear:

//Initialize listening method
private void listenMediaDB() {   
       if (mScreenObserver == null) {
            ScreensHandler screensHandler = new ScreensHandler();
            mScreenObserver = new ScreenshotContentObserver(screensHandler);
//Register to listen to the data change of image media library URL
private void registerContentObserver() {  
        if (mScreenObserver != null) {
            Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            getContentResolver().registerContentObserver(imageUri, false, mScreenObserver);
            mScreenObserver.setOnChangeListener(new ScreenshotContentObserver.onChangeListener() {
                public void onChange() {
                  //Refresh data and update UI here
            Log.i(TAG, "registerContentObserver!----------");
//Static inner class to prevent memory leakage
static class ScreensHandler extends Handler {
        public void handleMessage(Message msg) {
public void onDestroy() {
 	if (mScreenObserver != null) {
	//Unregistered listening
        mScreenObserver = null;

public class ScreenshotContentObserver extends ContentObserver {
    public static final String  TAG         = "ScreenshotContentObserver";
    private             int     mCount      = 0;
    public static final int     DATA_CHANGE = 0x110;
	private onChangeListener mOnChangeListener;
    public ScreenshotContentObserver(Handler handler) {
    public void onChange(boolean selfChange) {
     * onChange Medium response database
    public void onChange(boolean selfChange, Uri uri) {
        super.onChange(selfChange, uri);
        Log.d(TAG, "onChange() returned: uri = " + uri);
		//The URL of the screenshot is content://media/external/images/media
        if (uri.toString().contains("images")) {  
		//Once in a while, onChange2 times will occur after the screenshot. This can be done
           mCount += 1;
//         if (mCount == 1) {
            if (mOnChangeListener != null) {
//                }
	//After the screenshot refreshes the data, it can be set to 0;
    public void setOnChangeCount(int count) {
        this.mCount = count;
    public void setOnChangeListener(onChangeListener mOnChangeListener) {
        this.mOnChangeListener = mOnChangeListener;
    public interface onChangeListener {
        void onChange();

Posted by squizz on Tue, 05 May 2020 05:47:25 -0700