Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.0k views
in Technique[技术] by (71.8m points)

events - Android: How to detect a change in MediaStore when connected over MTP

I have big problems with MediaStore. I need to handle events when MediaStore is changed over MTP. I already have a receiver for android.intent.action.MEDIA_SCANNER_FINISHED action, but it is useful only for Universal Mass Storage(UMS). The scanner is not launched over MTP, because the MTP changes the MediaStore database directly.

Please would you be so kind and help me how to detect this events. Thank you very much for any help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I finally found a solution. I tried to use FileObserver but when you use it for all directories...it is quite memory consuming. So now I am using ContentObserver and it is working well:

public static class UriObserver
{
    private final Cursor mCursor;
    private final ContentObserver mObserver;
    private boolean mRunning = true;

    private class ObserverWithListener extends ContentObserver
    {
        private final OnChangeListener mListener;

        public ObserverWithListener(OnChangeListener listener)
        {
            super(new Handler());

            mListener = listener;
        }

        @Override
        public void onChange(boolean selfChange)
        {
            if (mRunning)
            {
                log.d("Change triggered");
                mListener.onChange();
            }
        }
    };

    public static UriObserver getInstance(ContentResolver contentResolver, Uri uri, OnChangeListener listener)
    {
        Cursor c = contentResolver.query(uri, new String[] { "*" }, null, null, null);

        if ((c = Dao.moveToFirst(c)) == null)
        {
            log.e("Cannot start observer for uri: " + uri);
            return null;
        }

        return new UriObserver(c, listener);
    }

    public UriObserver(Cursor c, final OnChangeListener listener)
    {
        mCursor = c;
        mObserver = new ObserverWithListener(listener);
        mCursor.registerContentObserver(mObserver);
    }

    public void stop()
    {
        mCursor.unregisterContentObserver(mObserver);
        Dao.closeCursor(mCursor);
        mRunning = false;
    }

    public interface OnChangeListener
    {
        public void onChange();
    }
}

The flag mRunning has to be there for some reason because onChange was sometimes called even if unregisterContentObserver() had been called before.

This code I am using with Uris that I want to observe, i.e. MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, ...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...