无法将 WakefulIntentService 的子类实例化为接收器
我正在使用 WakefulIntentService 和 AlarmManager 实现数据记录器服务。但是,我在实例化接收器时遇到问题。这是我尝试运行该应用程序时得到的结果。
错误/AndroidRuntime(3181):java.lang.RuntimeException:无法实例化接收器my.package.WakefulLoggerIntentService:java.lang.InstantiationException:my.package.WakefulLoggerIntentService
以下是服务类的相关部分:
public class WakefulLoggerIntentService extends WakefulIntentService
{
public WakefulLoggerIntentService()
{
super("WakefulLoggerIntentService");
}
@Override
protected void doWakefulWork( Intent intent )
{
// Do the actual data logging.
}
}
我已将接收器元素添加到我的清单:
<receiver android:name=".WakefulLoggerIntentService">
</receiver>
这是使用 AlarmManager 来安排日志记录事件的部分:
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);
我缺少什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WakefulLoggerIntentService
是一个服务
。它不是一个BroadcastReceiver
。要使用
WakefulIntentService
,您需要一个由闹钟触发的BroadcastReceiver
,然后调用sendWakefulWork()
来启动您的WakefulLoggerIntentService
。此外,您需要将
WakefulLoggerIntentService
在清单中注册为
,而不是
。WakefulLoggerIntentService
is aService
. It is not aBroadcastReceiver
.To use
WakefulIntentService
, you need aBroadcastReceiver
that is triggered by the alarm and, in turn, callssendWakefulWork()
to kick off yourWakefulLoggerIntentService
.Also, you need to have your
WakefulLoggerIntentService
be registered in the manifest as a<service>
, not a<receiver>
.