安卓 - 等待外部存储准备就绪

5
我目前正在开发一个读取外部存储的实时壁纸。当设备启动时,我假设实时壁纸可能在存储准备好之前就被启动了,尤其是如果它正在进行定期的错误检查。其他人报告了问题,我认为这就是原因。我似乎无法测试这个问题,因为外部存储在我的设备上似乎会立即挂载,而且我不确定如何强制它进行错误检查。所以我的第一个问题是,系统是否确实等待BOOT_COMPLETED意图再启动实时壁纸。
如果不是,那么等待外部存储准备好的正确方法是什么?我考虑在应用程序开始时调用类似以下的内容:
public void waitForExternalStorage()
{
    while(Environment.getExternalStorageState().equals(Environment.MEDIA_CHECKING))
    {
        try { Thread.sleep(1000L); }
        catch(InterruptedException e) { e.printStackTrace(); }
    }
}

在启动时,如果出现MEDIA_REMOVED -> MEDIA_UNMOUNTED -> MEDIA_CHECKING(optional) -> MEDIA_READY的情况,我是否需要检查其他情况?

3个回答

8
您可以注册一个BroadcastReceiver来监听外部存储状态的更改:
BroadcastReceiver externalStorageStateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        updateExternalStorageState();
    }
};

IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
registerReceiver(mExternalStorageReceiver, filter);
updateExternalStorageState(); // You can initialize the state here, before any change happens

updateExternalStorageState()中,您可以检查更改后的实际状态:

protected void updateExternalStorageState() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // SD card is mounted and ready for use
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // SD card is mounted, but it is read only
    } else {
        // SD card is unavailable
    }
}

我不确定如何使用这个。当壁纸加载时,需要读取外部存储器。我不能等待意图启动壁纸,因为系统会自动启动它。我是否需要暂停壁纸加载并等待意图恢复。而且如果没有外部存储,则意图不会触发。基本上,我需要的功能是:如果(没有外部存储)继续加载,如果(外部存储未准备好)等待,如果(外部存储已准备好)继续加载。 - terryhau
我只需要这些信息,谢谢 - 但是顺便提一下,类名的几个副本中有一个小错误(应该是BroadcastReceiver而不是BroadcastReciever)。SO不允许我编辑。 - just me

3

使用广播接收器,监听Intent.ACTION_MEDIA_MOUNTED:广播动作:外部存储介质已插入并挂载到其挂载点。


1

我正需要这个。我正在从SD卡中读取配置文件,我的应用程序在启动时必须读取它才能运行。一个简单的解决方案是等待Android操作系统自动挂载SD卡,最多等待15-30秒钟。如果没有成功挂载,就抛出异常。以下是代码。只需增加最大计数限制即可增加等待时间。ExternalStorageNotReadyException是自定义异常。

public void awaitExternalStorageInitialization()
        throws ExternalStorageNotReadyException {
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
    int count = 0;

    do {
        String state = Environment.getExternalStorageState();
        if(count > 0) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                Log.e(Constants.LOG_TAG, e.getMessage(), e);
            }
        }
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // We can read and write the media
            mExternalStorageAvailable = mExternalStorageWriteable = true;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            // We can only read the media
            mExternalStorageAvailable = true;
            mExternalStorageWriteable = false;
        } else {
            // Something else is wrong. It may be one of many other states,
            // but all we need
            // to know is we can neither read nor write
            mExternalStorageAvailable = mExternalStorageWriteable = false;
        }
        count++;
    } while ((!mExternalStorageAvailable) && (!mExternalStorageWriteable) && (count < 5));
    if(!mExternalStorageWriteable)
        throw new ExternalStorageNotReadyException("External storage device (SD Card) not yet ready");
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接