简体   繁体   English

服务开启时,如何强制应用退出省电模式?

[英]How force the app to opt out of battery saver mode when the service is ON?

The expected behavior is that the app will be running all the time when it's in ON state.预期的行为是应用程序在处于 ON 状态时将一直运行。 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)即使在使用代码后,默认状态也是 Battery Saver(推荐)

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.设置-->电池-->三点菜单项(...)--->电池优化-->(从列表中选择一个应用程序)--->优化/不优化。

By choosing Don't optimize you are essentially bypassing Doze , not app standby .通过选择Don't optimize您实际上绕过了Doze ,而不是app standby

Also be advised that doing this programmatically as you do may result in Google taking your app off the store .另请注意,以编程方式执行此操作可能会导致 Google 下架您的应用程序 It is safer to do it manually following the path i described above.按照我上面描述的路径手动执行更安全。

More on Doze and App Standby here此处了解有关Doze和 App Standby更多信息

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

相关问题 如何编写 jButton 以将笔记本电脑或设备切换到节电模式 - How to code a jButton to turn the laptop or device to battery saver mode 在省电模式 Android 上运行后台服务 - Run background services on battery saver mode Android 闹钟服务在手机的省电模式下不起作用 - alarm service not working in power saver mode of phone 以编程方式检查节电模式 - Checking for Power Saver Mode programmatically 如何使用BatteryStatsHelper获取应用程序电池信息? - How to use BatteryStatsHelper to get app battery information? 即使应用程序被强制停止,也要重新启动服务,即使关闭应用程序,也要在后台继续运行服务如何? - Restart the service even if app is force-stopped and Keep running service in background even after closing the app How? 即使应用程序被强制停止,也要重新启动服务;关闭应用程序后,仍要在后台运行服务。 - Restart the service even if app is force-stopped and Keep running service in background even after closing the app How? 如何检测电池电量低的时间:Android? - How to detect when the Battery's low : Android? 通过网站禁用屏幕保护程序/睡眠模式 - Disable screen saver / sleep mode through a website 从Android中的最新应用中刷出应用后,服务停止 - Service stopped when app is swiped out from recent apps in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM