相关疑难解决方法(0)

尝试在Android上启动服务

当设备在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" />
Run Code Online (Sandbox Code Playgroud)

广播接收器

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

android broadcastreceiver android-service

327
推荐指数
10
解决办法
19万
查看次数