Android O,后台服务运行了30分钟以上.为什么? [英] Android O, Background Service is running for more than 30 minutes. Why?

查看:178
本文介绍了Android O,后台服务运行了30分钟以上.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FusedLocationProvider API向PendingIntent注册一些Geofence,当我的应用通过Geofence事件启动(手动终止应用以触发此行为)时,我启动了后台服务来做一些工作.

I am using FusedLocationProvider API to register some Geofences with PendingIntent, when my app gets started (app was manually killed to trigger this behavior) via Geofence event, I start a background service to do some work.

在此服务中,我创建了一个单独的后台线程来执行一些长时间运行的任务,并且获得了唤醒锁,以确保我的任务已完成.该服务可以运行更长的时间(30-50分钟),即使它不是应该运行.根据我对Android O最新变化的理解,不应将其列入白名单,并使其长时间运行.

In this Service I create a separate background thread to do some long running tasks and I acquire a wake-lock so that I am sure my tasks are completed. The Service keeps running for longer period of times (30 - 50 minutes) even though It shouldn't be. It shouldn't be white-listed and keep itself running for longer period of times according to my understanding of recent changes in Android O.

使用PendingIntents唤醒应用程序是期望的行为还是某种错误,然后将其视为白名单,即使这些Intent不是用于通知的(即用户可以看到的任何内容).

Is it desired behavior or some kind of bug with PendingIntents waking up the app and then it's treated like white-listed even though those Intents are not for Notifications (i.e anything which user can see).

或者这些意图属于接收广播,例如SMS/MMS消息"的类别.如此处文档

Or these intents falls into category of "Receiving a broadcast, such as an SMS/MMS message." as mentioned here in documentation

推荐答案

我在预览中遇到了一些错误.

It my be some bug in preview.

但是,您确定1分钟后您的服务就会运行吗? 即使您的服务被销毁,您创建的应用程序进程和后台线程也仍将可以运行.

But, are you sure your service is running after 1 minute? Your app process and background thread you made will/could run even after service is destroyed.

因此,您的服务确实传递了onDestroy回调,但是您不会在其中停止线程并继续在后台进行工作.它只会在整个应用程序的进程被终止时停止,因为Android认为它不再重要了.

So your service does pass onDestroy callback, but you don't stop your thread in there and continue doing work in background. It only stops when whole app's process is killed because Android decided it's not important any more.

这篇关于Android O,后台服务运行了30分钟以上.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆