有沒有辦法得到手機當前運行的API版本? android
考慮到全部說法,這裏是用於檢測設備是否具備Froyo或更新Android OS(2.2+)的代碼: api
public static boolean froyoOrNewer() { // SDK_INT is introduced in 1.6 (API Level 4) so code referencing that would fail // Also we can't use SDK_INT since some modified ROMs play around with this value, RELEASE is most versatile variable if (android.os.Build.VERSION.RELEASE.startsWith("1.") || android.os.Build.VERSION.RELEASE.startsWith("2.0") || android.os.Build.VERSION.RELEASE.startsWith("2.1")) return false; return true; }
顯然,若是您須要通用檢查器,能夠修改if條件以考慮1.0和1.5版本的Android。 你可能會獲得這樣的東西: app
// returns true if current Android OS on device is >= verCode public static boolean androidMinimum(int verCode) { if (android.os.Build.VERSION.RELEASE.startsWith("1.0")) return verCode == 1; else if (android.os.Build.VERSION.RELEASE.startsWith("1.1")) { return verCode <= 2; } else if (android.os.Build.VERSION.RELEASE.startsWith("1.5")) { return verCode <= 3; } else { return android.os.Build.VERSION.SDK_INT >= verCode; } }
若是代碼不適合你,請告訴我。 ui
我更喜歡將版本做爲數字,比我寫的更簡單: this
public static float getAPIVerison() { Float f = null; try { StringBuilder strBuild = new StringBuilder(); strBuild.append(android.os.Build.VERSION.RELEASE.substring(0, 2)); f = new Float(strBuild.toString()); } catch (NumberFormatException e) { Log.e("", "error retriving api version" + e.getMessage()); } return f.floatValue(); }
我改進了我使用的代碼 spa
public static float getAPIVerison() { float f=1f; try { StringBuilder strBuild = new StringBuilder(); strBuild.append(android.os.Build.VERSION.RELEASE.substring(0, 2)); f= Float.valueOf(strBuild.toString()); } catch (NumberFormatException e) { Log.e("myApp", "error retriving api version" + e.getMessage()); } return f; }
獲得它了。 它使用Context
類的getApplicationInfo()
方法。 code
android.os.Build.VERSION.SDK應該爲您提供API級別的值。 你能夠在android文檔中輕鬆找到從api級到android版的映射。 我相信,8是2.2,7是2.1,依此類推。 orm