In Android applications, some hidden Google APIs (public classes, methods, or constants marked with @ hide) are often used to implement some special functions or effects, such as policymanager.
There are two main ways to use the android hidden API: 1. use the Java reflection mechanism to obtain hidden APIs through reflection; 2. fully compiled classes generated during source code compilation. jar package.
For example, if the following code is used directly, an error is returned.
mWindowManager = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE); mWindow = PolicyManager.makeNewWindow(mContext); mWindow.setWindowManager(mWindowManager, null, null);
1. Use the Java reflection mechanism and change it to the following code for normal use.
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);// using reflect mechanism to invoke hide apiString POLICYMANAGER_CLASS_NAME = "com.android.internal.policy.PolicyManager";try {Class policyClass;policyClass = Class.forName(POLICYMANAGER_CLASS_NAME);Log.i(TAG, policyClass.toString());Method meths[] = policyClass.getMethods();Method makenewwindow = null;// Method makenewwindow = policyClass.getMethod("makeNewWindow");for (int i = 0; i < meths.length; i++) {if (meths[i].getName().endsWith("makeNewWindow"))makenewwindow = meths[i];}Log.i(TAG, makenewwindow.toString());mWindow = (Window) makenewwindow.invoke(null, mContext);} catch (Exception e) {e.printStackTrace();}mWindow.setWindowManager(mWindowManager, null, null);
2. If you do not want to modify the code, use the second method: import the fully compiled classes. jar package. After the android framework source code is compiled, the fully compiled classes. jar is imported into the project. Use eclipse and Android to add a library (buildpath-> Add libraries-> User library-> New User library. add the JAR file to the library, and check the "systemlibrary" option to avoid "Java. lang. outofmemoryerror: Java
Heap space "error. If you have correctly imported the jar library, you still cannot find the hidden API. The reason may be that buildclass path order is incorrect, that is, android. jar and classes. jar import order is incorrect. For more information about how to adjust buildclass path order, select build path> config build path> order and export to adjust the custom library and Android. jar sequence.
At this point, the code at the beginning of this article takes effect without any changes.
The advantage of using the reflection mechanism to hide APIS is that it is flexible and can capture exceptions in incompatible systems without causing program crash. The disadvantage is that the process is too complicated, in addition, it is difficult to implement inheritance of hidden classes. If you need to use a large number of hidden APIs, using reflection will be annoying.
Import the fully compiled classes. the jar package is easy to use and program for hidden APIs, just like those hidden APIs become visible in the SDK. Its disadvantage is compatibility, and users are expected to pay attention to it.
The two methods have their own advantages and disadvantages. Please consider them. In general, the main reason why Google sets hidden APIS is that the Android system is still evolving and these APIs may not be mature and stable enough. Therefore, unless otherwise specified, you should minimize the use of hidden APIs.
Ref: http://zhmeup.iteye.com/blog/1119503