I am trying to create a condition in my code to compare Android versions.Something akin to;
if(开发者_Go百科version < 2.2)
{// TODO }
else
{// TODO }
Would anyone please let me know how to do this?
You can compare them as their integer version using Build.VERSION.SDK_INT
, so for SDK v 2.2 you will get integer with value 8
, while SDK v 3.0 you will get 11
.
And please look at this bunch of how to get android version question asked and answered again, I suggest you to use the search box, and glance to the related question provided while you are writing your question :)
Use This code:
int currentVersion = android.os.Build.VERSION.SDK_INT;
if (currentVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
// Do something for lollipop and above versions
} else{
// do something for phones running an SDK before lollipop
}
Ttry this:
private boolean compareVersion(String currrentsVersion, String officialVersion) {
isEqual = false;
String[] string1 = currrentsVersion.split("[.]");
String[] string2 = officialVersion.split("[.]");
Integer[] number2 = new Integer[string2.length];
Integer[] numbers = new Integer[string2.length];
for (int i = 0; i < string2.length; i++) {
if (string1.length-1<i)
numbers[i] = 0;
else
numbers[i] = Integer.parseInt(string1[i]);
System.out.println("number1 ::: " + numbers[i]);
}
for (int i = 0; i < string2.length; i++) {
number2[i] = Integer.parseInt(string2[i]);
System.out.println("number2 ::: " + number2[i]);
}
for (int i = 0; i < number2.length; i++) {
if (number2[i] > numbers[i]) {
isEqual = false;
break;
} else {
isEqual = true;
}
}
return isEqual;
}
Call this method where you want to compare the version.
if (compareVersion(getVersionInfo(), officialVersion))
tvUpdateStatus.setText("Have the latest version installed :) ");
else
tvUpdateStatus.setText("Plz Update the Application");
I hope it'll help. ;)
精彩评论