1

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?

Amos
  • 1,321
  • 2
  • 23
  • 44
  • Is your logcat output filtered? Can you try to add a breakpoint and run a debug – Tim Aug 22 '16 at 14:06
  • it is not filtered. it is running in debug mode but it never stops in the break point on this line Log.d("service","I'm in on Handle Intent"); – Amos Aug 22 '16 at 14:09
  • are you using `PedingIntent.getService` to generate the pending intent ? – Blackbelt Aug 22 '16 at 14:10
  • When I did, the whole thing was stuck, I then changed it to PendingIntent servicePendingIntent = PendingIntent.getActivity(context... and it started to work except it is not getting inside onHandleIntent – Amos Aug 22 '16 at 14:11
  • What do you mean *it was stuck*? You *should* be using the getService() method – Tim Aug 22 '16 at 14:12
  • The application stopped responding until it told me to close. Changing it to getActivity made it at least show progress. In a way :) – Amos Aug 22 '16 at 14:14
  • Oh well, I now changed it back to getService and it started to work – Amos Aug 22 '16 at 14:17
  • you can't use getActivity with a Service. – Blackbelt Aug 22 '16 at 14:20

1 Answers1

1

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);
LaurentY
  • 7,495
  • 3
  • 37
  • 55
  • I started with getService that stuck the app. I changed it to getActivity and it had the symptoms I described above. After a comment above stating the same as this answer, I changed it back to getService and it started to work – Amos Aug 22 '16 at 14:19
  • @Amos, cool, please accept if ti's useful – LaurentY Aug 22 '16 at 14:20