Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Android PowerProfile private API?

Tags:

android

I downloaded the source code from the below link and added 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 am getting and it can not find R file shown below.

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

Also can not import

import com.android.internal.util.XmlUtils;

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

Thanks in advance.

like image 289
codereviewanskquestions Avatar asked Nov 11 '11 22:11

codereviewanskquestions


4 Answers

Personally using patached android.jar just causes headaches, using reflection is a 'simple' way of accessing PowerProfile.java. But as @FoamyGuy and countless others have noted this is hidden api so wrap it in a big try catch as it could break on 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());
like image 189
scottyab Avatar answered Nov 12 '22 12:11

scottyab


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

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

like image 37
Aakash Avatar answered Nov 12 '22 14:11

Aakash


You could use

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

But be aware of what FoamyGuy commented.

like image 1
yonojoy Avatar answered Nov 12 '22 14:11

yonojoy


Easiest way to do this is to download the framework.jar of android and include that as external library in your project. After including the framework.jar in your android project you can find that resource file.

like image 1
Adi Tiwari Avatar answered Nov 12 '22 13:11

Adi Tiwari