Android IntentService won't start on boot

2019-03-22 08:51发布

问题:

I have an IntentService which starts on boot and stays running in the background. There is no launcher Activity, the IntentService is called by a broadcast receiver. It works on 2.3.4 and 4.0.4 devices but not on 4.2.2. Maybe my broadcast receiver isn't being triggered on the device?

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.xxx"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver android:name="br.com.xxx.RastreadorBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service
            android:name="br.com.xxx.ObtainAndPostLocationService"
            android:exported="true">
        </service>

    </application>

</manifest>

RastreadorBroadcastReceiver:

package br.com.xxx;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class RastreadorBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, ObtainAndPostLocationService.class);
        context.startService(startServiceIntent);
    }
}

回答1:

try changing

<receiver android:name="br.com.xxx.RastreadorBroadcastReceiver" >

in

<receiver android:name=".RastreadorBroadcastReceiver" >