How to Use hidden APIs in Android Projects

Source: Internet
Author: User

How to Use hidden APIs in Android Projects

Prerequisites for using android to hide APIs: We need to get a file compiled and output by the Android system source code.
Out \ target \ common \ obj \ JAVA_LIBRARIES \ framework_intermediates \ classes. jar
This package contains all system APIs, hidden and public
Add jar Method
Right-click the function menu and choose Properties> Java Build Path.

Libraries Tab
At this time, there should be a list. If you have not added it, there should be only one item, that is, the Android SDK that comes with the system. After selection, there will be a delete on the right hand side. First, delete the sdk added by the system.

Click Add Library-> User Library

Select the User Library button and create a new User Library to import the classes. jar file and the system file. Adjust the order and set classes. jar to the previous one.
After this is added, you can use the hidden api of the system.

To use the hidden api, you must:
Many APIs involve system permissions, such as the background Installation File api PackageManager. installPackage requires the permission to install the program. This permission is not available only for ROM signature authentication. although you can add this permission to the configuration file, the tragedy is that you still cannot have this permission. In this regard, Google is doing a great job ..

Okay. Although we cannot install the SDK, you can use the api to check the total apk size?
Google hasn't published this Api, but with the above method, we can use the file path of the // apk package.
String apkPath =
\ "/Sdcard/qq.apk \";
// This is a Package interpreter and is hidden.
// The constructor has only one parameter. The path of the apk File
PackageParser packageParser =
New PackageParser (apkPath );
// This is related to display, which involves pixel display and so on. We use the default situation.
DisplayMetrics metrics =
New DisplayMetrics ();
Metrics. setToDefaults ();
// Here is the resolution, four parameters,
// Source File,
// Target file path
// Display, DisplayMetrics metrics
// Flags
PackageParser. Package mPkgInfo = packageParser. parsePackage (new File (apkPath ),
ApkPath, metrics, 0 );

// Application information package, which is public, but some functions and variables are not public
ApplicationInfo info = mPkgInfo. applicationInfo;

// Resources are used to obtain Resources, and the Resources obtained here are outside of this program
Resources pRes = getResources ();
AssetManager assmgr =
New AssetManager ();
Assmgr. addAssetPath (apkPath); // This method is hidden. If the hidden API is not called, reflection is required before calling it.
Resources res =
New Resources (assmgr, pRes. getDisplayMetrics (), pRes. getConfiguration ());

CharSequence label =
Null;
If (info. labelRes! =
0 ){
Try {
Label = res. getText (info. labelRes );
} Catch (Resources. NotFoundException e ){
}
}
If (label =
Null ){
Label = (info. nonLocalizedLabel! =
Null )?
Info. nonLocalizedLabel: info. packageName;
}


// Here is the icon for reading an apk Program

If (info. icon! =
0 ){
Drawable icon = res. getDrawable (info. icon );
ImageView image = (ImageView) findViewById (R. id. iv_test );
Image. setVisibility (View. VISIBLE );
Image. setImageDrawable (icon );
}
}

========================================================== =====

Ps: By using hidden APIs, reflection calls are avoided.

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.