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