Android App忽略电池优化

忽略电池优化的设置

1、增加权限

"android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
复制代码

2、代码跳转设置,Activity中操作

    /**
     * 忽略电池优化
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    public void ignoreBatteryOptimization() {
        try{
            PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
            boolean hasIgnored = powerManager.isIgnoringBatteryOptimizations(this.getPackageName());
            if(!hasIgnored) {
                Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.parse("package:"+getPackageName()));
                startActivity(intent);
            }
        }catch(Exception e){
            //TODO :handle exception
            Log.e("ex",e.getMessage());
        }
    }
复制代码

转载于:https://juejin.im/post/5c1c9d48e51d455fac464c23

你可能感兴趣的:(Android App忽略电池优化)