Android app获取android.permission.BATTERY_STATS权限

记录下

1.手机root
2.Android工程AndroidManifest.xml声明
3.adb shell pm grant com.asksven.betterbatterystats android.permission.BATTERY_STATS

反射调用获取BatterStats信息

        Class mBatteryInfoClazz = Class.forName("com.android.internal.app.IBatteryStats$Stub");
        Class serviceManagerClazz = Class.forName("android.os.ServiceManager");
        Method getSevice = serviceManagerClazz.getMethod("getService", String.class);
        IBinder binder = ((IBinder) getSevice.invoke(null, "batterystats"));
        Log.e("batteryinfo","binder is " + (binder == null ? "null" : "not null"));
        Method asInterface = mBatteryInfoClazz.getMethod("asInterface",new Class[]{IBinder.class});
        Object object  = asInterface.invoke(null,binder);
        Method getStatistics = object.getClass().getMethod("getStatistics");
        byte[] data = ((byte[]) getStatistics.invoke(object));
        Log.e("batteryinfo",object.getClass().getName() + " data size " + data.length);

https://github.com/asksven/BetterBatteryStats/issues/716

你可能感兴趣的:(Android)