如何让Android Service在设备未唤醒的情况下运行?

发布于 2024-09-02 10:40:18 字数 253 浏览 3 评论 0原文

我的应用程序目标是每隔 20 分钟保存一次位置更新。

我使用了服务,它工作正常,但是当我锁定屏幕或它自动锁定时,服务停止运行。 当我解锁它时,服务再次运行。

突出显示我的代码:

Service()

onCreat(){
   call timer();
 }

timer(){
   code
 }

如何使我的代码在所有条件下始终运行?

my application objective is to save location updates every ,let say, 20 minuets .

I used service and it worked fine , but when i lock the screen or it is locked automatically the service stop running .
when i unlock it , service runs again.

highlight on my code:

Service()

onCreat(){
   call timer();
 }

timer(){
   code
 }

How to make my code run all the time in all conditions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

叫嚣ゝ 2024-09-09 10:40:19

是的,使用 AlarmManager 每 10 分钟启动一次服务,例如使用 setRepeating 如下所示。摆脱服务中的计时器,只让服务在 oncreate 或 onCommand 中运行任务开始完成。

    int SECS = 1000;
    int MINS = 60 * SECS;
    Calendar cal = Calendar.getInstance();
    Intent in = new Intent(context, YourService.class);
    PendingIntent pi = PendingIntent.getService(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarms = (AlarmManager)context.getSystemService(
            Context.ALARM_SERVICE);
    alarms.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
            10 * MINS, pi);

您可以创建一个活动,通过按钮在 onclick 处理程序中包含此代码以启动服务。如果您想在启动时运行,则需要将其放入广播接收器中,该接收器会在设备启动时收到通知,但这本身就是另一个主题。

当设备处于睡眠状态时,服务和计时器不起作用的原因是 cpu 关闭并且您的代码没有唤醒锁。 AlarmManager 会稍微唤醒 cpu 来运行您的服务。

Yes, use AlarmManager to start the service every 10 minutes for example with setRepeating like below. Get rid of timer in service and just let service run task in oncreate or onCommand start to completion.

    int SECS = 1000;
    int MINS = 60 * SECS;
    Calendar cal = Calendar.getInstance();
    Intent in = new Intent(context, YourService.class);
    PendingIntent pi = PendingIntent.getService(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarms = (AlarmManager)context.getSystemService(
            Context.ALARM_SERVICE);
    alarms.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
            10 * MINS, pi);

You can create an activity to contain this code in an onclick handler from a button to start the service. If you want to run at boot time you need to put this in a broadcast receiver that gets notified when device is up, but that is another topic on its own.

The reason service and timer does not work when device is asleep is because cpu is off and your code has no wake lock. AlarmManager will wake up the cpu ever so slightly to run your service.

維他命╮ 2024-09-09 10:40:19

安排您的服务按计划工作(例如,“调用计时器()”)(包括唤醒设备)的正确方法是使用 AlarmManager,可能与我的 AlarmManager 一起使用。 a href="http://github.com/commonsguy/cwac-wakeful" rel="nofollow noreferrer">WakefulIntentService 或类似的内容。

The right way to arrange for your service to do work on a scheduled basis (e.g., "call timer()"), including waking up the device, is to use the AlarmManager, probably along with my WakefulIntentService or something similar.

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