sd 完成加载后设备启动时运行服务

发布于 2024-12-17 14:42:24 字数 220 浏览 2 评论 0原文

我在设备启动时运行一项服务,该服务仅从 SD 卡加载图像并将其设为壁纸。当设备启动时,我收到错误消息。但在 SD 卡完成加载后,服务会将图像设置为壁纸。

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

我像这样启动服务^,我想问是否有办法在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" /> 

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

趁年轻赶紧闹 2024-12-24 14:42:25

编辑:我不知道 MEDIA_MOUNTED 的意图,我会使用下面发布的答案。

我要做的就是按照你拥有的方式启动服务,并在服务内部不断检查 SD 卡何时安装,也许每 300 毫秒或其他时间。这个问题展示了如何检测是否sdcard是否已挂载。

Edit: I didn't know there was a intent for MEDIA_MOUNTED, I would use the answer posted below instead.

What I would do is launch the service how you have it and inside of the service keep checking to see when the sdcard is mounted, maybe every 300ms or something. This question shows how to detect if the sdcard has been mounted or not.

染火枫林 2024-12-24 14:42:25

MEDIA_MOUNTED 操作将在此处起作用,但您必须在清单文件中包含它的意图过滤器,并且还必须将接收器意图过滤器的数据方案更改为“文件”。

    <receiver android:name="YourReceiver">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_MOUNTED"></action>
            <data android:scheme="file"/>
        </intent-filter>
    </receiver>

The MEDIA_MOUNTED action will work here, but you must include an intent filter for it in the manifest file, and you must also change the data scheme for the receiver's intent filter to "file" as well.

    <receiver android:name="YourReceiver">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_MOUNTED"></action>
            <data android:scheme="file"/>
        </intent-filter>
    </receiver>
海夕 2024-12-24 14:42:24

在安装媒体(SD 卡)并将操作设置为 MEDIA_MOUNTED

There's an intent broadcast for after media (SD card) is mounted with the action set to MEDIA_MOUNTED.

人生戏 2024-12-24 14:42:24

您可以等待 SD 卡加载。一种方法是使用 android.intent.action.MEDIA_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

希望这会有所帮助。

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

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文