我一直试图在设备在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");
    }
}

当前回答

如果在接收类中保留空构造函数,就会遇到这个问题。删除空构造函数后,onRreceive方法开始正常工作。

其他回答

我有一个额外的<category>-tag,不知道这是否有任何区别。

<receiver android:name="BootIntentReceiver">  
        <intent-filter>  
            <action android:name="android.intent.action.BOOT_COMPLETED" />  
            <category android:name="android.intent.category.HOME" />  
        </intent-filter>  
</receiver>

你有没有试过省略if子句"android.intent.action.BOOT_COMPLETED".equals(intent. getaction(),因为接收器可能只接收该意图?

正如@Damian评论的那样,这个帖子里的所有答案都是错误的。手动这样做会有服务在设备进入睡眠状态时中途停止的风险。你需要先获得一个尾流锁。幸运的是,支持库给了我们一个类来做这件事:

public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // This is the Intent to deliver to our service.
        Intent service = new Intent(context, SimpleWakefulService.class);

        // Start the service, keeping the device awake while it is launching.
        Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
        startWakefulService(context, service);
    }
}

然后,在你的服务中,确保释放唤醒锁:

    @Override
    protected void onHandleIntent(Intent intent) {
        // At this point SimpleWakefulReceiver is still holding a wake lock
        // for us.  We can do whatever we need to here and then tell it that
        // it can release the wakelock.

...
        Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
        SimpleWakefulReceiver.completeWakefulIntent(intent);
    }

不要忘记在mainfest中添加WAKE_LOCK权限:

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

在挂载外部存储之前,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.intent.action.BOOT_COMPLETED"权限和intent-filter,你实际上没有做错什么。

注意,如果你在Android 4。X,你必须在启动服务之前运行广播监听器,这意味着,你必须先添加一个活动,一旦你的广播接收器运行,你的应用程序应该像你预期的那样运行,然而,在Android 4上。X,我还没有找到一种方法来启动启动没有任何活动的服务,我认为谷歌这样做是出于安全原因。

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

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

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

希望能有所帮助。