Amos Amos - 3 months ago 14
Android Question

Android: IntentService starts but onHandleIntent is not executed

I want an IntentService to run every 10 seconds and put a simply debug line. This is my code:

in the manifest:

<service android:name="com.test.test.TheService" />


Creating the alarm manager to call the service every 10 seconds

am.setRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(),
10000,
servicePendingIntent);


The service itself

public class TheService extends IntentService {

public static int SERVICE_ID = 1;

public TheService() {
super("TheService");
}

@Override
protected void onHandleIntent(Intent intent) {
Log.d("service","I'm in on Handle Intent");
}
}


In the emulator's monitor I see this line every 10 seconds

1270-1297/system_process I/ActivityManager: START u0 {flg=0x4 cmp=com.test.test/.TheService (has extras)} from pid -1


Despite the fact it writes "has extras", I actually didn't add any extras. Generally, It seems fine but the debug is never printed and a breakpoint on the debug code, never stops as if the service does start every 10 seconds but it does nothing.

What am I missing?

Answer

You have to create an Intent to call your Service, and put it in PendingIntent with method getService(), as this:

Intent serviceIntent = new Intent(context, TheService.class);
PendingIntent pendingIntent = PendingIntent.getService(this,0, intent, 0);