Android developed by reflection get to Android hidden method

Source: Internet
Author: User

In Packagemanger, some methods are hidden, cannot be called directly, and you need to use reflection to get to the method.

For example: Getpackagesizeinfo (), this method can get to the APK cachesize,codesize,datasize and other information, but the method is hidden,@hide.

In this case, you need to use reflection:

 1  try   //  through reflection, get to Packagemanager hidden Method Getpackagesizeinfo ()  3  Method getpackagesizeinfo = Packagemanager. class . GetMethod ("Getpackagesizeinfo", String. Class , int . class , Ipackagestatsobserver. class   4 } catch   (Nosuchmethodexception e) {  E.printstacktrace ();  6 } 

Getpackagesizeinfo () Use after reflection:

1 getpackagesizeinfo.invoke (mPm, Appinfo.packagename, Myuserid.invoke (nullnull), Mystatsobserver);

Reflection parameter Description:

Packagemanager Source:

1      Public Abstract void int Userhandle, 2             Ipackagestatsobserver observer);

The source can be seen, the method requires three parameters, the first parameter is a string type of PackageName, the second argument is an int type of Userhandle, the third parameter is IPACKAGESTATSOBSERVER type Observer

So GetMethod () need to fill in the parameters:

The first parameter is the method name: "Getpackagesizeinfo"

The second parameter is the first parameter of Getpackagesizeinfo (): String.class

The third parameter is the second parameter of Getpackagesizeinfo (): Int.class

The fourth parameter is the third parameter of Getpackagesizeinfo (): Ipackagestatsobserver.class

This method can be obtained through reflection, and then the method is used, as follows:

Using the Invoke () method

First parameter: The class that executes the method, MPm

Second parameter: Package name, Appinfo.packagename

The third parameter: Userhandle.myuserid (), because the method is also hidden, so you need to use reflection to get to the method.

Fourth parameter: Ipackagestatsobserver object, Mystatsobserver

--------------------------------------------------------------------------------------------------------------- ---------------

The second method of reflection:

1 NULL ; 2 method[] methods = Packagemanager. class . GetMethods (); 3    for (Method m:methods) {4       if ("Getpackagesizeinfo". Equals (M.getname ())) {5            method = m; 6          }7       }

All methods of the class are obtained through GetMethods, and then the method is traversed, and the method name is used to determine whether the Getpackagesizeinfo () method is used, and then the method is assigned. Then you can use the method.

Android development by reflection get to Android hidden method

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.