美文网首页代码片段分享
Android 忽略系统电源优化 以保证后台运行

Android 忽略系统电源优化 以保证后台运行

作者: 花艺荣 | 来源:发表于2020-12-02 17:37 被阅读0次

    首先要说一下由于Android 系统UI的定制化严重,所以很多设备不能很好地匹配UI,或者
    设置成功后无法取消,但三星的设备表现还是中规中矩的。

    https://stackoverflow.com/posts/48859802/timeline

    This is part of the new App Standby feature introduced with API 23 (Marshmallow) alongside Doze Battery Optimization aimed to optimize power and resource usage while the app is in background (App Standby) or when the device has long been in sleep (Doze).

    Following is the explanation from the Android Developer's site page:

    Specifically, in the App Standby mode, the system determines that an app is idle when the user is not actively using it. The system makes this determination when the user does not touch the app for a certain period of time and none of the following conditions applies:

    • The user explicitly launches the app.
    • The app has a process currently in the foreground (either as an activity or foreground service, or in use by another activity or foreground service).
    • The app generates a notification that users see on the lock screen or in the notification tray.
    • The app is an active device admin app (for example, a device policy controller). Although they generally run in the background, device admin apps never enter App Standby because they must remain available to receive policy from a server at any time.

    When the user plugs the device into a power supply, the system releases apps from the standby state, allowing them to freely access the network and to execute any pending jobs and syncs. If the device is idle for long periods of time, the system allows idle apps network access around once a day.

    So, this means that starting from API 23 (Marshmallow), the device may actively put your app on standby, preventing network access (say for task like sync) and limiting (or disabling) background executions. Now, for most of the apps this behavior is fine or you could easily optimize for such behavior, but for some apps out there this may cause some unexpected behavior, especially for apps that have been poorly optimized or use non-standard sync strategies or some other means of background sync/execution. So to circumvent that, the user can explicitly mark the app as non-optimized and the system will fallback and not put the app to standby, although this leads to a poorer user experience and you should not be doing this for regular apps that could be optimized.

    resolvent

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

    Also, you need to add the following permission in manifest

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

    相关文章

      网友评论

        本文标题:Android 忽略系统电源优化 以保证后台运行

        本文链接:https://www.haomeiwen.com/subject/upjuwktx.html