服务可以在几个小时内正常工作,而不是没有任何响应
当我在 Gmail 客户端中收到电子邮件时,我想在 GmailService 类中运行 CippaLippa() 方法。
我在 AndroidManifest 中有一个接收器和一个服务...
<receiver
android:name="com.myapp.receiver.GmailReceiver">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="com.myapp.service.GmailService"
android:label="@string/app_name" />
以及这些类...
public class GmailReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
final SharedPreferences preferences = context.getSharedPreferences("myapp.prefs", 0);
context.startService(new Intent(context, GmailService.class));
}
}
public class GmailService extends Service { .. etc...}
我的问题:在几个小时内一切正常,当我收到来自 Gmail 的通知时,CippaLippa() 方法会触发...然后,几个小时后,当我收到 Gmail 通知时,CippaLippa() 方法不再触发。
也许,有一种方法可以告诉 GmailService 类“保持活动状态”并继续监视 Gmail 事件?我认为这不是由于 Android 操作系统杀死了未使用的类,因为那是后台服务而不是 Activity。 我不知道。
I want to run a method CippaLippa() in the GmailService class when I receive an email in Gmail client.
I've a receiver and a service in AndroidManifest...
<receiver
android:name="com.myapp.receiver.GmailReceiver">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="com.myapp.service.GmailService"
android:label="@string/app_name" />
and these classes...
public class GmailReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
final SharedPreferences preferences = context.getSharedPreferences("myapp.prefs", 0);
context.startService(new Intent(context, GmailService.class));
}
}
public class GmailService extends Service { .. etc...}
My question: everything works right for some hours and when I receive a notification from Gmail, the CippaLippa() method fires... then, after some hours, when I receive a gmail notification, the CippaLippa() method fires no more.
Maybe, there is a way to tell GmailService class to "stay alive" and continue monitoring Gmail events? I think this is not due to Android OS that kills unused classes, because that is a background service and not an Activity.
I've no clue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下代码将立即启动警报管理器(触发 PendingIntent)并每 60 秒运行一次任务。
The following code will start the alarm manager immediately (fire the PendingIntent) and run the task every 60 seconds.
您关于系统杀死服务的说法是正确的。我在与小部件关联的服务上遇到了同样的问题。
我通过让它每 30 分钟左右刷新一次来解决这个问题。
因此,如果您偶尔安排一些事件来唤醒您的服务,它应该保持正常运行。
不过可能有更好的解决方案。
You're right about the system killing the service. I've had the same problem with a service associated with a widget.
I solved it by making it refresh every 30 minutes or so.
So if you schedule some event once in a while to wake up your service, it should stay up and running.
There might be a better solution though.
请查看 START_STICKY。我不确定这是否完全有效,但它可能有助于保持您的服务持续进行。
Have a look at START_STICKY. I'm not sure if this will entirely do the trick, but it may help keep your service going.