服务可以在几个小时内正常工作,而不是没有任何响应

发布于 2024-10-30 18:01:04 字数 1170 浏览 6 评论 0原文

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

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

发布评论

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

评论(3

给不了的爱 2024-11-06 18:01:04

以下代码将立即启动警报管理器(触发 PendingIntent)并每 60 秒运行一次任务。

    Intent logReader = new Intent();
    logReader.setClassName("com.foo.Test", "com.foo.Test.MyServiceClass");
    logReaderPI = PendingIntent.getService(context, 0, logReader, 0);
    long firstTime = SystemClock.elapsedRealtime();
    AlarmManager aMgr =  (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    aMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 60 * 1000, logReaderPI);

The following code will start the alarm manager immediately (fire the PendingIntent) and run the task every 60 seconds.

    Intent logReader = new Intent();
    logReader.setClassName("com.foo.Test", "com.foo.Test.MyServiceClass");
    logReaderPI = PendingIntent.getService(context, 0, logReader, 0);
    long firstTime = SystemClock.elapsedRealtime();
    AlarmManager aMgr =  (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    aMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 60 * 1000, logReaderPI);
ゞ花落谁相伴 2024-11-06 18:01:04

您关于系统杀死服务的说法是正确的。我在与小部件关联的服务上遇到了同样的问题。
我通过让它每 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.

歌枕肩 2024-11-06 18:01:04

请查看 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.

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