我一直试图在设备在android上启动时启动一项服务,但我无法让它工作。我在网上看了一些链接,但没有一个代码工作。我是不是忘了什么?

AndroidManifest.xml

<receiver
    android:name=".StartServiceAtBootReceiver"
    android:enabled="true"
    android:exported="false"
    android:label="StartServiceAtBootReceiver" >
    <intent-filter>
        <action android:name="android.intent.action._BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service
    android:name="com.test.RunService"
    android:enabled="true" />

BroadcastReceiver

public void onReceive(Context context, Intent intent) {
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent serviceLauncher = new Intent(context, RunService.class);
        context.startService(serviceLauncher);
        Log.v("TEST", "Service loaded at start");
    }
}

当前回答

我刚刚发现,可能是因为设置>电源中的快速启动选项

当我关闭这个选项时,我的应用程序接收到一个this广播,而不是其他。

顺便说一下,我在HTC Incredible S上安装了Android 2.3.3。

希望能有所帮助。

其他回答

如果你正在使用Android Studio,你非常喜欢自动完成,那么我必须告诉你,我正在使用Android Studio v 1.1.0,我使用自动完成以下权限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Android Studio Auto-completedRECEIVE_BOOT_COMPLETED都是小写的,比如receive_boot_completed,我一直在拉我的头发,因为我已经列出了我的清单,为启动服务在启动时做的事情。我刚刚再次确认

Android Studio会自动用小写完成此权限。

在挂载外部存储之前,BOOT_COMPLETE被发送执行。如果你的应用被安装到外部存储,它不会收到BOOT_COMPLETE广播消息。为了防止这种情况,可以将应用程序安装在内部存储中。只需在menifest.xml中添加这一行即可

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="internalOnly"
... >

一些HTC设备可以启用“快速启动”功能,这更像是深度休眠,而不是真正的重新启动,因此不应该给出BOOT_COMPLETE意图。为了恢复这个,你可以在你的接收器中添加这个intent过滤器:

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

请注意,在问题的开头,有一个拼写错误:

<行动android: name = " android.intent.action._BOOT_COMPLETED " / >

而不是:

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

一个小“_”和所有这些麻烦:)

随着

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

也使用,

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

HTC设备似乎没有捕捉BOOT_COMPLETED

参考此链接http://khurramitdeveloper.blogspot.in/2013/06/start-activity-or-service-on-boot.html 在服务上使用引导的步骤