Use of hidden APIs and internal packages in Android to get the app power rating

Source: Internet
Author: User

Today, the boss to arrange a task called me to get the application of the mobile phone line (time is 7 o'clock the day before the morning of 10), so in various search on the Internet, did not expect this kind of information is still a lot of, found a main class:powerprofile, but can be, This class is not exposed to our developers, so we can't call it

What about that? , there is a way, this class is hidden in:com.android.internal.os.PowerProfile

Com.android.internal.os. Powerprofile. Powerprofile This class is responsible for parsing the record file, we can create an object of it, through which to obtain specific information. Specific power consumption calculation is more complex

Take a look at the case: demo is the 2.3.3 of the settings source code modified

(Demo: http://download.csdn.net/detail/jiangwei0910410003/7357717)

Mainly for those who have not studied this part of the source of the students.
The principle of getting the battery rank in the settings is realized by reading the battery record file. The main use of android.os.BatteryStats this hidden class and com.android.internal this special package.
Hidden classes are good solution, mainly internal package, the package not only Android.jar not, in time you find the package, put it into the Android.jar, you will find it is also not used. The reason is that ADT has imposed a limit on this package. Not allowed to use.


One solution is to modify ADT to remove the restriction. (This approach is difficult to achieve)
The other is not to put internal into the Android.jar, and put him alone as a package into the project, so it will not be limited


We use the second solution, which is also the simplest

is to import this package directly into our project, you can use the hidden API and internal package in the project.
We can use the package to make a separate platform, which we'll talk about later.

Note: The package cannot be placed directly inside the Libs directory, please place it elsewhere. Because ADT shields the internal package, it is directly placed under Libs and the ADT detects it. The files that are placed on the outside are not packaged in our apk file, so we can also reduce the size of the APK file. In the same vein, the non-system package must be placed under the Libs, or the program will be running when the report can not find the package error.


Here's a look at how to make this jar package:

First we need to understand why the hidden API (with the @hide tag) and the internal package are not available.

When we use the Android SDK for development, we will use a very important JAR file--android.jar (Android SDK directory/platforms/platform-x/android.jar,x is the API level). This package removes all tagged tail @hide classes, methods, enumerations, fields, and internal packages . When our program runs on the device, it loads a Framework.jar file on the device that contains the removed part.


So our idea is to find a way to get Framework.jar to get those missing APIs out and put them in Android.jar. Of course, these can be compiled source code to solve, but that is too much trouble. Let me show you an easy way to get these things.


1, Get Framework.jar
As I said earlier, there is this file on the device, so we'll take it from the device. via ADB pull command (also available with DDMS):
ADB Pull/system/framework/framework.jar
We need the Classes.dex file in the package. Here from 2.3.3 below the simulator is taken, the real machine is generally optimized for. Odex files, and there are no Classes.dex files in this file in the emulator above 2.3.3. Note: The Classes.dex file is not available in the real machine, so get it to the simulator!


2, the Framework.jar changed to Framework.zip, decompression, get inside the Classes.dex file.


3. Convert. dex files to. jar format. This tool is used by Dex2jar, who knows that the anti-compilation students know this tool:

http://download.csdn.net/detail/jiangwei0910410003/7346527

A Classes.dex.dex2jar.jar file is obtained when the conversion is complete.


4, rename Classes.dex.dex2jar.jar into Classes.dex.dex2jar.zip, unzip.


5. Copy the Android.jar from the Android SDK directory/platforms/platform-x/and open it with WinRAR.



It is important to note that we add files in Classes-dex2.jar based on Android.jar, and remember to choose the Update method: Skip the existing file:



Then click OK, this time Android.jar is the jar we need, the above operation some people may be a bit strange, why not the files are extracted first, and then merged in compression into zip, here we do not know why, anyway, I did before, the final synthesis of the jar words, There is a problem with the use, so this method is tangled up.

At the same time here must remember the choice is the zip way, or will error:



OK, Finish.

Here's a look at:


This socket is our application, power consumption is still possible ...


Summary: There are many APIs in Android that we can't use, and then I'll talk about how to use the technology of reflection to access the hidden and access-restricted APIs, which is very useful, and will be used a lot later.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.