這是關於使用反射來獲取BatteryStatsHelper的代碼。使用反射來獲取BatteryStatsHelper.getUsageList()
try {
batteryStatsHelperClass = Class.forName("com.android.internal.os.BatteryStatsHelper");
Constructor constructor = batteryStatsHelperClass.getConstructor(Context.class);
Method getUsageList = batteryStatsHelperClass.getDeclaredMethod("getUsageList");
getUsageList.setAccessible(true);
List list = (List) getUsageList.invoke(batteryStatsHelper);
Log.d(TAG, "size: " + list.size());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
列表中我得到的是空的,我想知道這種方式是真實的,爲什麼我不能得到的數據?
這是BatteryStatshelper的源代碼。
public BatteryStatsHelper(Context context) {
this(context, true);
}
public BatteryStatsHelper(Context context, boolean collectBatteryBroadcast) {
this(context, collectBatteryBroadcast, checkWifiOnly(context));
}
public BatteryStatsHelper(Context context, boolean collectBatteryBroadcast, boolean wifiOnly) {
mContext = context;
mCollectBatteryBroadcast = collectBatteryBroadcast;
mWifiOnly = wifiOnly;
}
..................
public List<BatterySipper> getUsageList() {
return mUsageList;
}