获取Android版本

Posted on 2013-09-16 10:26  香蕉菊花小哥  阅读(358)  评论(0编辑  收藏  举报

JAVA:

摘自http://stackoverflow.com/questions/3423754/retrieving-android-api-version-programmatically

从1.6开始,

int currentapiVersion = android.os.Build.VERSION.SDK_INT;

if (currentapiVersion >= android.os.Build.VERSION_CODES.FROYO){
    // Do something for froyo and above versions
} else{
    // do something for phones running an SDK before froyo
}

 相关代号宏:

http://developer.android.com/reference/android/os/Build.VERSION.html#SDK_INT

http://developer.android.com/reference/android/os/Build.VERSION_CODES.html

 

JNI C/C++:

摘自:http://stackoverflow.com/questions/10196361/how-to-check-the-device-running-api-level-using-c-code-via-ndk

 1 // Based on article here:
 2     //   http://android-developers.blogspot.co.uk/2011/09/androids-http-clients.html
 3     // Which references the issue documented here:
 4     //   http://code.google.com/p/android/issues/detail?id=2939
 5     // We need to set "http.keepAlive" to "false" if running an OS version earlier than Froyo (API Level 8)
 6 
 7     if ((*env)->ExceptionCheck(env))
 8         return false; // already got an exception pending
 9 
10     bool success = true;
11 
12     // VERSION is a nested class within android.os.Build (hence "$" rather than "/")
13     jclass versionClass = (*env)->FindClass(env, "android/os/Build$VERSION");
14     if (NULL == versionClass)
15         success = false;
16 
17     jfieldID sdkIntFieldID = NULL;
18     if (success)
19         success = (NULL != (sdkIntFieldID = (*env)->GetStaticFieldID(env, versionClass, "SDK_INT", "I")));
20 
21     jint sdkInt = 0;
22     if (success)
23     {
24         sdkInt = (*env)->GetStaticIntField(env, versionClass, sdkIntFieldID);
25         __android_log_print(ANDROID_LOG_VERBOSE, TAG, "sdkInt = %d", sdkInt);
26     }
27 
28     if (success && sdkInt < 8)
29     {
30         jclass systemClass = (*env)->FindClass(env, "java/lang/System");
31         if (NULL == systemClass)
32             success = false;
33 
34         jmethodID setPropertyMethodID = NULL;
35         if (success)
36             success = (NULL != (setPropertyMethodID = (*env)->GetStaticMethodID(env, systemClass, "setProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;")));
37 
38         jstring propString = NULL;
39         if (success)
40             success = (NULL != (propString = (*env)->NewStringUTF(env, "http.keepAlive")));
41 
42         jstring valueString = NULL;
43         if (success)
44             success = (NULL != (valueString = (*env)->NewStringUTF(env, "false")));
45 
46         jobject oldValueString = NULL;
47         if (success)
48         {
49             __android_log_print(ANDROID_LOG_VERBOSE, TAG, "Disabling http.keepAlive");
50              oldValueString = (*env)->CallStaticObjectMethod(env, systemClass, setPropertyMethodID, propString, valueString);
51         }
52 
53         // cleanup
54         (*env)->DeleteLocalRef(env, propString);
55         (*env)->DeleteLocalRef(env, valueString);
56         (*env)->DeleteLocalRef(env, oldValueString);
57         (*env)->DeleteLocalRef(env, systemClass);
58     }
59 
60     // cleanup
61     (*env)->DeleteLocalRef(env, versionClass);
62 
63     return success;