问题描述
我有很大的问题,MediaStore。我需要处理事件时,MediaStore改变了MTP。我已经有一个接收器, android.intent.action.MEDIA_SCANNER_FINISHED
的行动,但它仅适用于通用海量存储(UMS)是有用的。该扫描仪没有推出过MTP,因为中期计划直接改变MediaStore数据库。
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!
推荐答案
我终于找到了解决办法。我试图用FileObserver但是当你使用它的所有目录......这是相当消耗内存。所以,现在我使用ContentObserver,它运作良好:
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();
}
}
标志mRunning已经在那里出于某种原因,因为的onChange有时被称为即使unregisterContentObserver()被调用之前。
The flag mRunning has to be there for some reason because onChange was sometimes called even if unregisterContentObserver() had been called before.
这code,我使用与我想观察尤里斯,也就是MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,...
This code I am using with Uris that I want to observe, i.e. MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, ...
这篇关于Android的:如何检测MediaStore改变连接了中期计划时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!