How to use Android PowerProfile private API?

I downloaded the source code from the link below and added it to my project.

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/com/android/internal/os/PowerProfile.java

I get and it cannot find the R file shown below.

int id = com.android.internal.R.xml.power_profile; 

Also cannot be imported

 import com.android.internal.util.XmlUtils; 

I mainly want to measure the power consumption of Android devices.

Thanks in advance.

+6
source share
4 answers

Yes, you can access the internal API, which is com.android.internal.os.PowerProfile

just take a look at this link and follow step by step.

+2
source

Personally, using patached android.jar just causes headaches; using reflection is the "easy" way to access PowerProfile.java. But as @FoamyGuy and many others pointed out that this is a hidden api, so wrap it in a big attempt to catch, as it might break on a later version of Android.

 Class<?> powerProfileClazz = Class.forName("com.android.internal.os.PowerProfile"); //get constructor that takes a context object Class[] argTypes = { Context.class }; Constructor constructor = powerProfileClazz .getDeclaredConstructor(argTypes); Object[] arguments = { context }; //Instantiate Object powerProInstance = constructor.newInstance(arguments); //define method Method batteryCap = powerProfileClazz.getMethod("getBatteryCapacity", null); //call method Log.d(TAG, batteryCap.invoke(powerProInstance, null).toString()); 
+3
source

you can use

 int id = Resources.getSystem().getIdentifier("power_profile", "xml", "android"); 

But remember what FoamyGuy commented on.

+1
source

The easiest way to do this is to download the framework.jar android and include it as an external library in your project. After including framework.jar in your Android project, you can find this resource file.

+1
source

Source: https://habr.com/ru/post/901325/


All Articles