* Suppose there's a simple new call you want to use, like android.os.Debug.dumpHprofData(String filename). The Debug class has existed since Android 1.0, but the method is new in Anroid 1.5 (API Level 3). If you try to call it directly, your app will fail to run on devices running Android 1.1 or earlier.
* The simplest way to call the method is through reflection.
- This requires doing a one-time lookup and caching the result in a Method object.
- Using the method is a matter of calling Method.invoke and un-boxing the result.
* Using a wrapper class
- The idea is to create a class that wraps all of the new APIs exposed by a new or existing class.
- Each method in the wrapper class just calls through to the corresponding real method and returns the same result.
* If the target class or method doesn't exist, the initialization of the wrapper class fails, and your application knows that it should avoid using the newer calls.
* When wrapping an existing class that now has new methods, you only need to put the new methods in the wrapper class. Invoke the old methods directly. The static initializer in WrapNewClass would be augmented to do a one-time check with reflection.
(?)
* Reference
- Backward Compatibility for Applications
沒有留言:
張貼留言