服务开启时,如何强制应用选择退出省电模式? [英] How force the app to opt out of battery saver mode when the service is ON?

查看:142
本文介绍了服务开启时,如何强制应用选择退出省电模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预期的行为是,该应用在处于ON状态时将一直运行.当应用程序一段时间未激活时,某些电话会将其置于后台模式.我希望该应用程序即使在待机模式下也始终运行(待机模式意味着当我们按下主屏幕按钮时,该应用程序将进入后台运行一段时间.)

The expected behavior is that the app will be running all the time when it's in ON state. Some phones put the app in background mode when the app is not active for some time. I want the app to be running all the time even its in standby mode(standby mode means when we press the home button the app will go to background. and it will run for some time).

我找到了以下代码,并尝试了

I found following code and I tried that

    PowerManager powerManager = (PowerManager) getApplicationContext().getSystemService(POWER_SERVICE);
    String packageName = "org.traccar.client";
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Intent i = new Intent();
        if (!powerManager.isIgnoringBatteryOptimizations(packageName)) {
            i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            i.setData(Uri.parse("package:" + packageName));
            startActivity(i);
        }
        else{
            i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            i.setData(Uri.parse("package:" + packageName));
            startActivity(i);

        }
    }

即使使用该代码后,默认状态也为省电模式(推荐)

Even after working with the code the default state is Battery Saver(recommended)

打开应用后,我希望该应用处于无限制模式,对此有什么解决办法?

I want the app in No Restriction mode once the app is opened, any solution for this?

推荐答案

您使用的代码用于电池优化.设置->电池->三点菜单项(...)--->电池优化->(从列表中选择一个应用程序)--->优化/不优化.

The code you use is for battery optimization. Settings-->Batery-->Three Dots Menu Item (...)--->Battery Optimization-->(Choose an app from list)--->Optimize/ Don't optimize.

通过选择不优化,您实际上是在绕过 Doze ,而不是 app Standby .

By choosing Don't optimize you are essentially bypassing Doze, not app standby.

还建议您像这样以编程方式执行此操作,可能会导致Google退出您的应用.按照我上面描述的路径手动进行操作比较安全.

Also be advised that doing this programmatically as you do may result in Google taking your app off the store. It is safer to do it manually following the path i described above.

关于打ze 和应用程序 Standby 的更多信息此处

More on Doze and App Standby here

这篇关于服务开启时,如何强制应用选择退出省电模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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