In actual programming, if the code needs to consider the android1/2 API at the same time, how can we achieve compatibility in the same code?
In fact, there are good examples in the android2.2 Development Kit sample (/apidemos/src/COM/example/Android/APIs/APP/foregroundservice. Java.
Here, we mainly consider the newly added interfaces in the service class of Android 2.2.
Startforeground (int id, notification)
Stopforeground (Boolean removenotification)
try { mStartForeground = getClass().getMethod("startForeground", mStartForegroundSignature); mStopForeground = getClass().getMethod("stopForeground", mStopForegroundSignature); } catch (NoSuchMethodException e) { // Running on an older platform. mStartForeground = mStopForeground = null; }
At the beginning, I was not very familiar with this Code. Why do I need to turn a bend and call it through reflection. The later code is clear, to be compatible with the android 1 code. That is, if mstartforeground/mstopforeground is null, it indicates the android1 environment.
This method is also frequently used in actual work, so that client code does not depend on APIs.
There is another problem here. If it is to determine whether the API has a newly added class, do not know whether this method is feasible?