Check whether the battery optimization is disabled for the application - Android Hardware

Android examples for Hardware:Battery

Description

Check whether the battery optimization is disabled for the application

Demo Code


//package com.java2s;
import android.annotation.TargetApi;
import android.content.Context;

import android.os.Build;
import android.os.PowerManager;

public class Main {
    /**/*from   w  ww .jav  a  2s  .c  om*/
     * Check whether the battery optimization is disabled for the application
     *
     * @param context
     * @return
     */
    @TargetApi(Build.VERSION_CODES.M)
    //
    public static boolean isBatteryOptimizationDisabled(Context context) {
        String packageName = context.getPackageName();
        PowerManager pm = (PowerManager) context
                .getSystemService(Context.POWER_SERVICE);
        return pm.isIgnoringBatteryOptimizations(packageName);
    }
}

Related Tutorials