get Power Save Status - Android Android OS

Android examples for Android OS:Power

Description

get Power Save Status

Demo Code


//package com.java2s;
import android.app.Application;
import android.content.Context;

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

public class Main {
    public static boolean getPowerSaveStatus() {
        if (Build.VERSION.SDK_INT >= 21) {
            try {
                Context context = getApplicationUsingReflection()
                        .getApplicationContext();
                PowerManager pm = (PowerManager) context
                        .getSystemService(Context.POWER_SERVICE);
                return pm.isPowerSaveMode();
            } catch (Exception e) {
                System.out.println(e);
            }/*from   www  . jav a 2s.co m*/
        }
        return false;
    }

    public static Application getApplicationUsingReflection()
            throws Exception {
        return (Application) Class.forName("android.app.ActivityThread")
                .getMethod("currentApplication")
                .invoke(null, (Object[]) null);
    }
}

Related Tutorials