无法将 WakefulIntentService 的子类实例化为接收器

发布于 2024-12-06 21:48:25 字数 1165 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愿得七秒忆 2024-12-13 21:48:25

WakefulLoggerIntentService 是一个服务。它不是一个BroadcastReceiver

要使用 WakefulIntentService,您需要一个由闹钟触发的 BroadcastReceiver,然后调用 sendWakefulWork() 来启动您的 WakefulLoggerIntentService

此外,您需要将 WakefulLoggerIntentService 在清单中注册为 ,而不是

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>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文