标签:
import java.lang.reflect.Field; import java.lang.reflect.Method; import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.IBinder; import android.os.PowerManager; import android.os.SystemClock; import android.util.Log; //注意,PowerManager只有系统应用才能操作,普通应用不能操作,所以下面代码仅供参考 public class PowerUtil { private final static String TAG = "PowerUtil"; private static int getValue(Context ctx, String methodName, int defValue) { int value = defValue; PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE); try { Class<?> pmClass = Class.forName(pm.getClass().getName()); Field field = pmClass.getDeclaredField("mService"); field.setAccessible(true); Object iPM = field.get(pm); Class<?> iPMClass = Class.forName(iPM.getClass().getName()); Method method = iPMClass.getDeclaredMethod(methodName); method.setAccessible(true); value = (Integer) method.invoke(iPM); } catch (Exception e) { e.printStackTrace(); } Log.d(TAG, "methodName="+methodName+", value="+value); return value; } public static int getMinLight(Context ctx) { return getValue(ctx, "getMinimumScreenBrightnessSetting", 0); } public static int getMaxLight(Context ctx) { return getValue(ctx, "getMaximumScreenBrightnessSetting", 255); } public static int getDefLight(Context ctx) { return getValue(ctx, "getDefaultScreenBrightnessSetting", 100); } //设置屏幕亮度。light取值0-255 public static void setLight(Context ctx, int light) { PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE); try { Class<?> pmClass = Class.forName(pm.getClass().getName()); // 得到PowerManager类中的成员mService(mService为PowerManagerService类型) Field field = pmClass.getDeclaredField("mService"); field.setAccessible(true); // 实例化mService Object iPM = field.get(pm); // 得到PowerManagerService对应的Class对象 Class<?> iPMClass = Class.forName(iPM.getClass().getName()); /* * 得到PowerManagerService的函数setBacklightBrightness对应的Method对象, * PowerManager的函数setBacklightBrightness实现在PowerManagerService中 */ Method method = iPMClass.getDeclaredMethod("setBacklightBrightness", int.class); method.setAccessible(true); // 调用实现PowerManagerService的setBacklightBrightness method.invoke(iPM, light); } catch (Exception e) { e.printStackTrace(); } } public static void resetLight(Context ctx, int light) { try { Object power; Class <?> ServiceManager = Class.forName("android.os.ServiceManager"); Class <?> Stub = Class.forName("android.os.IPowerManager$Stub"); Method getService = ServiceManager.getMethod("getService", new Class[] {String.class}); //Method asInterface = GetStub.getMethod("asInterface", new Class[] {IBinder.class});//of this class? Method asInterface = Stub.getMethod("asInterface", new Class[] {IBinder.class}); //of this class? IBinder iBinder = (IBinder) getService.invoke(null, new Object[] {Context.POWER_SERVICE});// power = asInterface.invoke(null,iBinder);//or call constructor Stub?// Method setBacklightBrightness = power.getClass().getMethod("setBacklightBrightness", new Class[]{int.class}); setBacklightBrightness.invoke(power, new Object[]{light}); } catch (Exception e) { e.printStackTrace(); } } //锁屏 public static void lockScreen(Context ctx) { PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE); pm.goToSleep(SystemClock.uptimeMillis()); } //解锁 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public static void unLockScreen(Context ctx) { PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE); pm.wakeUp(SystemClock.uptimeMillis()); } //重启 public static void reboot(Context ctx) { PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE); pm.reboot(null); } //关机 public static void shutDown(Context ctx) { Intent intent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN"); intent.putExtra("android.intent.extra.KEY_CONFIRM", false); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 弹出系统内置的对话框,选择确定关机或取消关机 ctx.startActivity(intent); } }
import com.example.exmbattery.util.DateUtils; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.os.Bundle; import android.widget.TextView; public class BatteryActivity extends Activity { private TextView tv_battery_change; private static TextView tv_power_status; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_battery); tv_battery_change = (TextView) findViewById(R.id.tv_battery_change); tv_power_status = (TextView) findViewById(R.id.tv_power_status); } @Override protected void onStart() { super.onStart(); batteryChangeReceiver = new BatteryChangeReceiver(); IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); registerReceiver(batteryChangeReceiver, filter); } @Override protected void onStop() { super.onStop(); unregisterReceiver(batteryChangeReceiver); } private BatteryChangeReceiver batteryChangeReceiver; private class BatteryChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent != null) { int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0); int healthy = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0); int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0); int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 3); String technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY); int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0); boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false); String desc = String.format("%s : 收到广播:%s", DateUtils.getNowDateTime(), intent.getAction()); desc = String.format("%s\n电量刻度=%d", desc, scale); desc = String.format("%s\n当前电量=%d", desc, level); desc = String.format("%s\n当前状态=%s", desc, mStatus[status]); desc = String.format("%s\n健康程度=%s", desc, mHealthy[healthy]); desc = String.format("%s\n当前电压=%d", desc, voltage); desc = String.format("%s\n当前电源=%s", desc, mPlugged[plugged]); desc = String.format("%s\n当前技术=%s", desc, technology); desc = String.format("%s\n当前温度=%d", desc, temperature/10); desc = String.format("%s\n是否提供电池=%s", desc, present?"是":"否"); tv_battery_change.setText(desc); } } } private static String[] mStatus = {"不存在", "未知", "正在充电", "正在断电", "不在充电", "充满"}; private static String[] mHealthy = {"不存在", "未知", "良好", "过热", "坏了", "短路", "未知错误", "冷却"}; private static String[] mPlugged = {"电池", "充电器", "USB", "不存在", "无线"}; private static String mChange = ""; public static class PowerChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent != null) { mChange = String.format("%s\n%s : 收到广播:%s", mChange, DateUtils.getNowDateTime(), intent.getAction()); tv_power_status.setText(mChange); } } } }
import com.example.exmbattery.util.DateUtils; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class LockScreenReceiver extends BroadcastReceiver { private static final String TAG = "LockScreenReceiver"; @Override public void onReceive(Context context, Intent intent) { if (intent != null) { String mChange = ""; mChange = String.format("%s\n%s : 收到广播:%s", mChange, DateUtils.getNowDateTime(), intent.getAction()); if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { mChange = String.format("%s\n这是屏幕点亮事件", mChange); } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { mChange = String.format("%s\n这是屏幕关闭事件", mChange); } else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) { mChange = String.format("%s\n这是用户解锁事件", mChange); } Log.d(TAG, mChange); MainApplication.getInstance().setChangeDesc(mChange); } } }
import android.app.Application; import android.content.Intent; import android.content.IntentFilter; public class MainApplication extends Application { private static MainApplication mApp; private LockScreenReceiver mReceiver; private String mChange = ""; public static MainApplication getInstance() { return mApp; } public String getChangeDesc() { return mApp.mChange; } public void setChangeDesc(String change) { mApp.mChange = mApp.mChange + change; } @Override public void onCreate() { super.onCreate(); mApp = this; mReceiver = new LockScreenReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_USER_PRESENT); registerReceiver(mReceiver, filter); } @Override public void onTerminate() { unregisterReceiver(mReceiver); super.onTerminate(); } }
import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class ScreenActivity extends Activity { private static TextView tv_screen; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_screen); tv_screen = (TextView) findViewById(R.id.tv_screen); } @Override protected void onStart() { super.onStart(); tv_screen.setText(MainApplication.getInstance().getChangeDesc()); } }
标签:
原文地址:http://blog.csdn.net/aqi00/article/details/52095301