Unable to instantiate subclass of WakefulIntentService as a receiver
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.ge开发者_C百科tBroadcast(getApplicationContext(), 0, i, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 30000, pi);
What am I missing?
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>
.
精彩评论