0

I'm implementing a datalogger service using WakefulIntentService and AlarmManager. I'm having trouble instantiating the receiver, however. Here's what I'm getting when I try to run the app.

ERROR/AndroidRuntime(3181): java.lang.RuntimeException: Unable to instantiate receiver my.package.WakefulLoggerIntentService: java.lang.InstantiationException: my.package.WakefulLoggerIntentService

Here's the relevant parts of the service class:

public class WakefulLoggerIntentService extends WakefulIntentService
{
   public WakefulLoggerIntentService()
   {
      super("WakefulLoggerIntentService");
   }

   @Override
   protected void doWakefulWork( Intent intent )
   {
      // Do the actual data logging.
   }
}

I've added receiver element to my manifest:

<receiver android:name=".WakefulLoggerIntentService">
</receiver>

And here's the part that uses AlarmManager to schedule the logging events:

  Intent i = new Intent(getApplicationContext(), WakefulLoggerIntentService.class);
  PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0);
  AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
  am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 30000, pi);

What am I missing?

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Janne Oksanen
  • 1,524
  • 1
  • 13
  • 14

1 Answers1

0

WakefulLoggerIntentService is a Service. It is not a BroadcastReceiver.

To use WakefulIntentService, you need a BroadcastReceiver that is triggered by the alarm and, in turn, calls sendWakefulWork() to kick off your WakefulLoggerIntentService.

Also, you need to have your WakefulLoggerIntentService be registered in the manifest as a <service>, not a <receiver>.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491