Android under Get not installed APK contains information etc.

Source: Internet
Author: User

In many cases, we need to get some information about it through the apk file. (This apk is not necessarily installed at this time)

0. Basic knowledge:
A Packagemanager instance can be obtained by Android.content.Context Method Getpackagemanager ().
Packagemanager has a method:
PackageInfo Getpackagearchiveinfo (String archivefilepath, int flags);
Where parameter one: Archivefilepath. means an absolute path to an APK file.

Note The return value of this. It's a packageinfo. It contains all the information pakcage, and it can be thought that this information is collected from the Androidmanifest.xml.



1. Get the activity information in the APK:
PackageInfo includes: Publicactivityinfo[] activities
It will get all <Activity> tags in <application> in androidmanifest.xml.

For example:

<application android:label= "@string/app_name" android:debuggable= "true" > <activity android:name= ". Hellojni " android:label= "@string/app_name" ><intent-filter> <action android:name= "Android.intent.action.MAIN"/> <categor Y android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity></application>

Activityinfo: Information obtained from Androidmanifest.xml ' s <activity> and <receiver> tags.


All information related to the activity can be obtained from this item. This content is obtained from <activity> or <receiver>.

Example 1: Get the activity information:
To get activity information, the parameter 2 is set to: Packagemanager.get_activities
PackageInfo PI = Pm.getpackagearchiveinfo ("/data/hellojni.apk", packagemanager.get_activities);

// the PackageName obtained in PackageInfoLOG.W ("3DiJoy", "PackageInfo". PackageName: "+ pi.packagename); LOG.W ("3DiJoy", String.Format ("There is [%d] Activity", PI.activities.length)); LOG.W ("3DiJoy", String.Format ("Activity in porcess Name: [%s]", pi.activities[0].processname));

Note: The first time you see processname here:
The name of the process this component should run in. From the ' android:process ' attribute or,//if not set, the same as Applicationinfo.processname
That is, this item is obtained from Main.xml and is the content of the Android:process project. If not set, it is the same as applicationinfo.processname. (note 1)

The packagename where the Activity resides. Should be the same as the PackageName obtained in pockageinfo above.

LOG.W ("3DiJoy", "Activity in the package PackageName:" + pi.activities[0].packagename);
Activity name:<activity android:name= ". Hellojni ". Read in the
LOG.W ("3DiJoy", "Activity Name:" + pi.activities[0].name);



2. Get application information in APK:
Information collected from the <application> tag, or null if there is none.
Hehe, as previously mentioned, in fact, all the items in PackageInfo are taken from the main.xml, the previous activity so, now application is the same.

There is one item in the PackageInfo:
Public ApplicationInfo ApplicationInfo

LOG.W ("3DiJoy", "appliction datadir:" + PI.applicationInfo.dataDir); LOG.W ("3DiJoy", "Application SourceDir:" + PI.applicationInfo.sourceDir);//LOG.W ("3DiJoy", "Application JNI libdir:" + PI.applicationInfo.nativeLibraryDir); LOG.W ("3DiJoy", "Application Permission:" + PI.applicationInfo.permission); LOG.W ("3DiJoy", "Application in Porcessname is:" + PI.applicationInfo.processName);// (note 2)LOG.W ("3DiJoy", String.Format ("Minsdk Version: [%d]", PI.applicationInfo.targetSdkVersion)); LOG.W ("3DiJoy", "Application Name:" + PI.applicationInfo.name); LOG.W ("3DiJoy", "Application in Package" + PI.applicationInfo.packageName);





3. Get the APK version information:

<manifest> tag ' s "name" attribute.
LOG.W ("3DiJoy", "PackageInfo". PackageName: "+ pi.packagename);

LOG.W ("3DiJoy", "PackageInfo". Version String: "+ pi.versionname); LOG.W ("3DiJoy", String.Format ("Version code:[%d]", Pi.versioncode));
Separate from: <manifest> Tagged ' s versioncode attribute and <manifest> tag ' s versionname attribute to get information.











Note 1:
ProcessName Related content:
Pi.activities[0].processname . It's actually Activityinfo's parent class: Android.content.pm.ComponentInfo

The member variable. Refers to the name of the process that this activity is running on.
This information is obtained from the Main.xml "android:process" attribute. If not set, it is the same as applicationinfo.processname.


NOTE 2:
PI.applicationInfo.processName
The processname that this application will run is obtained from the Main.xml "process" attribute. If not set, it should be the same as PackageName.


Note 3:
PI.applicationInfo.packageName
The PackageName where application is located.


But Sam found:
Under the Android2.2.
ProcessName is obtained from activity and is null. (
Pi.activities[0].processname ) getting ProcessName from application is also empty。 ( PI.applicationInfo.processName)
but it's right to get packagename from application.( PI.applicationInfo.packageName, with Pi.packagenameSame



Note 4:
Getpackagearchiveinfo() parameter 1, archive refers to the apk file.

1) Get the APK file icon

[Java]View Plaincopyprint?
  1. Public static drawable Getapkfileicon (String Apkpath, Context context) {
  2. Packagemanager pm = Context.getpackagemanager ();
  3. PackageInfo info = pm.getpackagearchiveinfo (Apkpath,
  4. Packagemanager.get_activities);
  5. if (info! = null) {
  6. ApplicationInfo appInfo = Info.applicationinfo;
  7. if (appInfo! = null) {
  8. try {
  9. Appinfo.publicsourcedir = Apkpath;
  10. return Pm.getapplicationicon (AppInfo);
  11. } catch (OutOfMemoryError e) {
  12. }
  13. }
  14. }
  15. return null;
  16. }


2) Get the values defined in the <meta-data> tag in Androidmanifest.xml

[Java]View Plaincopyprint?
  1. Packagemanager manager = This.getpackagemanager ();
  2. try {
  3. //Setting the Packagemanager.get_meta_data identity bit is required
  4. PackageInfo info = manager.getpackageinfo (this.getpackagename (),
  5. packagemanager.get_configurations | Packagemanager.get_meta_data);
  6. Object MyChannel = Info.applicationInfo.metaData.get ("Meta-data-key");
  7. if ((mychannel! = null) && (MyChannel instanceof Integer)) {
  8. //Do something
  9. }
  10. } catch (Namenotfoundexception e) {
  11. E.printstacktrace ();
  12. }

Android under Get not installed APK contains information etc.

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.