I h开发者_JAVA技巧ave a question concerning Android programming. I intend to programm an application for Androiod 2.2 and above (including tablets). Problem, I am facing, is, that various version have different API news, e.g. Download Manager is fully implemented for the first time in 2.3, in 2.2 I have to programm it manually. I am thinking of building my app from modules, where each of them would be for particular Android OS version. You can imagine it as main app and depending on actual OS, it triggers Android 2.3 API Download Manager or (in case of 2.2) it starts my custom download manager, I have made.
Problem is, how to solve it in code? Because, when I enter to my 2.2 project .java file with Download manager import and code with it's usage, I cannot build application. I have found DexClassLoader solution, where I install only core of an app and other "modules" I will read from external sources depending on API level of current device. It sounds to me like "dirty" approach, isn't there anything "cleaner" how to solve particular pieces of code, which differs according to API level?
Thank ya
There is a way to check, if a class (e.g. DownloadManager) is available on the running system by trying to load the class like this:
try {
Class.forName(className);
} catch (ClassNotFoundException e) {
}
If the exception is thrown, you know the class is not available. Then you have to use your own class. On the other side, if the class is available, you have to use a separate class, to use it.
So the classes, that contain calls to an api, that is not available on certain systems is never loaded on not supported systems.
Of course, in this case you have to use a TargetSDK, that supports the classes you want to implement.
精彩评论