Rohil Verma
Rohil Verma

Reputation: 45

How do I set my service's priority to ensure that it starts at bootup?

I'm trying to start my service at bootup, but I keep getting the 1245-1263/? I/ActivityManager﹕ Waited long enough for: ServiceRecord{3d8aa3b0 u9 myPackage/.myService} exception.

I've looked at this: My service always getting Waited long enough for: ServiceRecord error in Kitkat which told me that the ActivityManager is putting starting my service on hold. I've confirmed through log statements that my receiver is indeed working. Here is the code in case anyone is interested:

public class PhoneStateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("OnStartUp", "Service is about to be started");
        Intent myIntent = new Intent(context, myService.class);
        context.startService(myIntent);
    }
}

So, how can I set my service's priority to "essential" so that Android makes sure it starts? I looked at this: How to set the priority of android service? but it deals with restarting a service rather than the initial startup.

Here's the relevant bit of my manifest:

<receiver android:name=".PhoneStateReceiver"
    android:enabled="true"
    android:label="PhoneStateReceiver"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>
<service
    android:name=".myService"
    android:enabled="true"
    android:exported="true" >
</service>

Thanks in advance.

Upvotes: 2

Views: 2063

Answers (2)

Ridcully
Ridcully

Reputation: 23655

You have to register a bootup-completed broadcast receiver and from there start your service. Here is a working implementation. Perhaps its the <uses-permission> part you're missing?

EDIT

In my manifest (important parts) I have that <uses-permission> part at the beginning.

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

    <!-- for services (must be rescheduled after boot) -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <receiver android:name=".services.BootCompletedReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

</manifest>

My receiver class looks like this. Note, I'm starting an IntentService.

public class BootCompletedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent msgIntent = new Intent(context, ReminderService.class);
        msgIntent.setAction(ReminderService.ACTION_RESCHEDULE);
        context.startService(msgIntent);
    }
}

Upvotes: 2

neonDion
neonDion

Reputation: 2358

From the code that is posted, it looks like the manifest is looking for a service called .myService and in the BroadcastREceiver you're trying to start a service called LocationService.class.

This could be the issue.

Upvotes: 0

Related Questions