设备启动时运行的服务的SD完成加载后 [英] Run a service when device starts after sd finishes loading

查看:93
本文介绍了设备启动时运行的服务的SD完成加载后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时启动设备,而该服务只是加载从SD卡的图像,并使其壁纸的服务。当装置启动时我得到一个错误。但SD卡完成装载后,该服务设置图像作为壁纸罚款。

I run a service when the device boots and that service just loads an image from sd card and makes it wallpaper. When the device starts i get an error. But after the sd card finishes the loading the service sets the image as wallpaper fine.

<action android:name="android.intent.action.BOOT_COMPLETED" /> 

我启动该服务,这样的^,我想询问是否有SD卡加载完毕后启动该服务的方法。

I start the service like this ^ and i want to ask if there is a way to start the service after sd card finishes loading.

推荐答案

您可以等待SD卡装入。一种方法是使用 android.intent.action.MEDIA_MOUNTED 行动

You can wait for the SD card to load. One way is to use the android.intent.action.MEDIA_MOUNTED action.

的另一种方法是轮询高达一些最大和放弃,如果未安装

Another way is to poll up to some maximum and give up if not mounted:

String mountState = Environment.getExternalStorageState();
int tries = 15;
do {
    if (!mountState.equals(Environment.MEDIA_MOUNTED)) {
        Log.i(LOG_TAG, "External media present but not mounted. Waiting 15 seconds for mount...");
        try {
            Thread.sleep(1000); // sleep for a second
        } catch (InterruptedException e) {
            Log.w(LOG_TAG, "Interrupted!");
            break;
        }
        mountState = Environment.getExternalStorageState();
    } else {
        Log.i(LOG_TAG, "External media mounted");
        break;
    }
} while (--tries > 0);
if (tries == 0) // give up

希望这有助于。

这篇关于设备启动时运行的服务的SD完成加载后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆