Android:当 SD 卡真正安装且可读时的意图
我有以下设置:
SD 卡安装接收器(MountReceiver.java)
public IntentFilter getIntentFilter() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.addDataScheme("file");
return intentFilter;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
setMounted(true);
} else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
setMounted(false);
}
}
private void setMounted(boolean isMounted) {
if (isMounted) {
doPerformQuery();
}
}
当我注册接收器时,我会:
registerReceiver(mountInstance, mountInstance.getIntentFilter());
一切都很好,并且按预期工作。我可以卸载并重新安装我的 SD 卡,并且我的 setMounted 方法会根据需要触发。我遇到的问题是,安装 SD 卡后,我的 doPerformQuery() 方法返回 0 结果。如果我将代码更改为:
Thread.sleep(5000);
doPerformQuery();
那么它偶尔会起作用。这给了 SD 卡 5 秒的时间来完成“准备”。我想知道SD卡什么时候准备完成并且可以读取。目前,我在安装 SD 卡时得到了意图,但它还没有“准备好”,所以它不可读。是否有这样的意图,当SD卡完成“准备”时进行注册。附带说明一下,我说“准备”是因为我的手机通知栏中就是这么说的。我不确定这是否是所有手机的标准,因为我找不到任何有关在线准备 SD 卡的信息。
I have the following setup:
SD card Mount Receiver (MountReceiver.java)
public IntentFilter getIntentFilter() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.addDataScheme("file");
return intentFilter;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
setMounted(true);
} else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
setMounted(false);
}
}
private void setMounted(boolean isMounted) {
if (isMounted) {
doPerformQuery();
}
}
And when I register the receiver, I do:
registerReceiver(mountInstance, mountInstance.getIntentFilter());
Everything is fine and dandy and working as expected. I can unmount and remount my SD card and my setMounted method is fired as appropriate. The problem I have is, is that my doPerformQuery() method returns 0 results after the SD card is mounted. If I change the code to:
Thread.sleep(5000);
doPerformQuery();
Then it will work, occasionally. This gives the SD card 5 seconds to finish "preparing". I want to know when the SD card has finished preparing and can be read. Currently, I'm getting the intent when the SD card is mounted, but it hasn't "prepared" itself yet, so it's not readable. Is there such an intent to register to when the SD card has finished "preparing". On a side note, I say "preparing" because that's what my phone says in the notification bar. I'm not sure if this is standard across all phones as I can't find a single thing about the SD card preparing online.
回应我们对您最初问题的评论...
我认为逻辑是,当卸下 SD 卡时,各种 AV 媒体(视频、音乐、照片等)的内容提供程序将清除 SD 卡上的任何内容,因为系统不会知道同一张 SD 卡何时(如果有)会返回,以及如果返回,上面是否会有相同的文件。因此,每次安装 SD 卡时都会执行完整扫描(导致“正在准备”通知)。
因此,在您的情况下,检查 ACTION_MEDIA_SCANNER_STARTED 以及最重要的是关联的 ACTION_MEDIA_SCANNER_FINISHED 意味着 AV 媒体内容提供商将拥有卡上所有内容的当前列表。
In response to our comments to your original question...
I think the logic is that when the SD card is dismounted, the content provider for various AV media (videos, music, photos etc) is cleared of anything on the SD card because the system won't know when (if ever) the same SD card will return and if it does whether it will have the same files on it. As a result, a full scan is carried out each time an SD card is installed (resulting in the 'preparing' notification).
So in your case, checking for ACTION_MEDIA_SCANNER_STARTED and most importantly the associated ACTION_MEDIA_SCANNER_FINISHED means the AV media content provider will have a current list of everything on the card.