简体   繁体   English

在省电模式 Android 上运行后台服务

[英]Run background services on battery saver mode Android

I have a service for sending http request that runs on background and works fine on "normal mode".我有一个发送 http 请求的服务,该请求在后台运行并在“正常模式”下正常工作。 The problem is when I put my phone on "battery saver mode", the service not working.问题是当我将手机置于“省电模式”时,该服务无法正常工作。 But, apps like Whatsapp still working.但是,像 Whatsapp 这样的应用程序仍然有效。 Do you have any idea what's going on?你知道发生了什么吗?

Mobile applications like Whatsapp must be requesting permission to exempt them from Doze/battery saving and App standby mode.像 Whatsapp 这样的移动应用程序必须请求许可才能免于打瞌睡/电池节省和应用程序待机模式。

It is possible to configure this manually by configuring the Whitelist in Settings > Battery > Battery Optimization .可以通过在Settings > Battery > Battery Optimization 中配置白名单来手动配置。

Alternatively from API 23, you can use permissions model to request users to whitelist them (refer this ).或者从 API 23 开始,您可以使用权限模型来请求用户将他们列入白名单(请参阅)。 From API You can also check whether your app is currently on White list by calling isIgnoringBatteryOptimizations()从 API 您还可以通过调用isIgnoringBatteryOptimizations()检查您的应用当前是否在白名单中

However you need to satisfy certain criteria for being able to whitelist yourself.但是,您需要满足某些标准才能将自己列入白名单。 Otherwise you face issues while maintaining app on Google Play Store.否则,您在 Google Play 商店中维护应用程序时会遇到问题。

But mostly messenger apps like Whatsapp are triggered through high-priority push notifications so they are more likely to be active despite not running background process.但是大多数像 Whatsapp 这样的信使应用程序是通过高优先级推送通知触发的,因此尽管没有运行后台进程,它们更有可能处于活动状态。

Add permission添加权限

<uses-permission 
android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>

request whitelist your app请求将您的应用列入白名单

 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Intent intent = new Intent();
            String packageName = getPackageName();
            PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
            if (!pm.isIgnoringBatteryOptimizations(packageName)) {
                intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.parse("package:" + packageName));
                startActivity(intent);
            }
        }

The background services are being worked on since Marshmallow and are not to be used for anything else than foreground usage since Oreo.自 Marshmallow 以来,后台服务正在开发中,自 Oreo 以来,除了前台使用外,不得用于任何其他用途。

The doze mode will forbide any operation while active, the phone will get out of doze for few Ms every X hours, some library will allow you to make some operations during that time.打瞌睡模式会在激活时禁止任何操作,手机每隔 X 小时会退出打盹几毫秒,某些库会允许您在此期间进行一些操作。

If you want to do some background operation during the doze & Battery saver you may want to have a look at the JobScheduler for operation that will be executed every 15 minutes or more while the phone is not in doze and if you require operations more often, i would advice you to have a look at the JobDispatcher .如果您想在打盹和省电模式期间进行一些后台操作,您可能需要查看JobScheduler的操作,该操作将在手机未处于打瞌睡状态时每 15 分钟或更长时间执行一次,如果您需要更频繁的操作,我建议你看看JobDispatcher Both will execute your task when going out of the doze mode退出打盹模式时,两者都会执行您的任务

Using workers will enable your application to run in the doze windows but network calls will be forbidden.使用 worker 将使您的应用程序能够在瞌睡窗口中运行,但将禁止网络调用。 Therefore you need to request for ignore battery optimization so that you can make network calls.因此,您需要请求忽略电池优化,以便您可以拨打网络电话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM