how to compare Android versions - android

I am trying to create a condition in my code to compare Android versions.Something akin to;
if(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. ;)

Related

Finding App Architecture using android API

Suppose we can use getPackageInfo in Package Manager in android and get any installed app's versionCode and all.
So can we find the architecture or the app? Like it's arm-v7a or arm64
I'll be very helpful to you.
Thank you
I found your question for "Java custom string encryption from a paragraph or set of strings", and would like to help because I've done something similar in c#.
I noticed that one of the largest reasons for that question being closed is because you were lacking in certain details that would help us determine what you needed.
This is an example of mine:
public static string Trans(string data) {
data = ToBinary(ConvertToByteArray(data));
string[] Adata = data.Split();
string output = "";
foreach(string word in Adata) {
int count = 0;
bool counting1s = false;
count = 0;
output = "";
foreach(var ch in word) {
if (ch == '0' && counting1s) {
counting1s = false;
if (count > 0) {
output += bit1[count - 1];
count = 0;
}
}
if (ch == '1' && !counting1s) {
counting1s = true;
if (count > 0) {
output += bit0[count - 1];
count = 0;
}
}
count++;
}
if (count > 0) {
output += (counting1s ? bit1 : bit0)[count - 1];
}
}
return output;
}
I could help you with making something in Java if youd like.

Rating bar like android in codename one

I want to add Add Raring Bar in a Form of codename one like android.. But am afraid there is no GUI for to create in codename one.. Is there any other option for to create it..
I think someone contributed a component like that on the discussion forum once, but I can't find the link.
It should be relatively simple to create using something like this (didn't test this code though):
Container starSelect = new Container(new BoxLayout(BoxLayout.X_AXIS));
for(int iter = 0 ; iter < 5 ; iter++) {
createStarButton(starSelect);
}
void createStarButton(final Container parent) {
final CheckBox cb = new CheckBox();
cb.setToggle(true);
cb.setIcon(unselectedStarIcon);
cb.setPressedIcon(selectedStarIcon);
cb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
if(cb.isSelected()) {
boolean selected = true;
for(int iter = 0 ; iter < parent.getComponentCount() ; iter++) {
Component current = parent.getComponentAt(iter);
if(current == cb) {
selected = false;
continue;
}
((CheckBox)cb).setSelected(selected);
}
}
}
});
parent.addComponent(cb);
}
I'am not iOS developer but i can provide some links please try
1.DLStarRating
2.RatingBar
3.XLRatingBar
4.Sample RatingBar Project

Android Java code is giving Syntax error on break statement

I need help on figuring out why this code is giving me:
Syntax error on token ":", { expected after this token/line
108,109,110(label125,label192,label202 in the beginning)/Java Problem
public void onGPSUpdate(Location paramLocation)
{
if (paramLocation != null)
{
this.sampleCount = (1 + this.sampleCount);
label125: int i;
label192: int j;
label202: int m;
if (paramLocation.hasSpeed())
{
this.speed = (2.23693629D * paramLocation.getSpeed());
this.oldLocation = paramLocation;
this.mAvgSum += this.speed;
this.speedQ.add(Double.valueOf(this.speed));
if (this.speedQ.size() > 300)
this.mAvgSum -= ((Double)this.speedQ.remove()).doubleValue();
if (!this.speedQ.isEmpty())
break label324;
this.mAvg = 0.0D;
if (this.speed <= this.speedThreshold)
break label363;
this.THRESHOLD_STATUS = ("OVER: " + this.speedThreshold + " MPH");
if (!this.smsControlRunning)
{
smsControlOn();
this.smsControlRunning = true;
}
if (!this.blueToothPaired)
break label346;
i = 0;
if (!this.hasBlueTooth)
break label351;
j = 0;
int k = i | j;
if (!this.phoneControlRunning)
break label357;
m = 0;
label218: if ((k & m) != 0)
{
phoneControlOn();
this.phoneControlRunning = true;
}
}
while (true)
{
this.SPEED_STATUS = String.valueOf(this.speed);
this.COUNT_STATUS = (this.sampleCount + " updates");
this.MAVG_STATUS = ("mAvg: " + this.mAvg);
return;
this.speed = getSpeed(this.oldLocation, paramLocation);
break;
label324: this.mAvg = (this.mAvgSum / this.speedQ.size());
break label125;
label346: i = 1;
break label192;
label351: j = 1;
break label202;
label357: m = 1;
break label218;
label363: this.THRESHOLD_STATUS = ("UNDER: " + this.speedThreshold + " MPH");
stopAllServices();
}
}
Toast.makeText(this, "LOCATION NULL", 0).show();
}
This is probably an obvious error but I am relatively new to Android development and using Eclipse. The same code worked fine under a previous version before renaming(refactoring) the application. This was coded by another programmer who worked on the project before me and I think this could be done better using a case or switch statement instead of the break and continue we have. I am getting frustrated with it and would appreciate any advice.
Try using those labels for "real" code lines.
You're using them at variable declarations, so no real point to go.
Use somthing like...
int i;
int j;
int m;
label125: i=0;
label192: j=0;
label202: m=0;
if (paramLocation.hasSpeed())

Comparing two strings in android [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Comparing two identical strings with == returns false
I am having real hard time with solving this code. This might look silly but I am not able to figure out what is happening. I am parsing a JSONArray(No big deal!) I am traversing the array with a for loop. I want to break the loop when the user input and the value matches. Here is my code
String regi = null;
JSONObject studentObject = null;
try {
JSONArray returned = test.getInternetData();
int i;
for (i = 0; i < returned.length(); i++) {
studentObject = returned.getJSONObject(i);
regi = studentObject.getString("REGISTRATION_NO");
if (regi == reg) {
name = studentObject.getString("STUDENT_NAME");
break;
}
}
course = studentObject.getString("COURSE_CODE");
Log.d("Details", name + course + regi + i);//Out put: nullGraduate081018394:name - null
//course: Graduate (same for all), regi: last registration number,
//i: giving totalnumber of objects
As per my knowledge the loop should stop when it finds a match. The COURSE_CODE will be corresponding to the student. Am I missing something?
Please note: The function getInternetData() is returning the whole JSON Array. The loop is completely traversing every object.
Strings cannot be compared with == in Java. You have to use string1.equals(string2).
Use regi.equals(reg) or regi.contentEquals(reg) instead of == and you will be fine :-)
use regi.contentEquals(reg) or !regi.contentEquals(reg) for comparison
you should use regi.contentEquals(reg)
try using this
JSONArray returned = test.getInternetData();
int i;
for (i = 0; i < returned.length(); i++) {
// added the below line
studentObject = new JsonObject();
studentObject = returned.getJSONObject(i);
regi = studentObject.getString("REGISTRATION_NO");
if (regi.equals(reg)) {
name = studentObject.getString("STUDENT_NAME");
break;
}
}
instead of just
JSONArray returned = test.getInternetData();
int i;
for (i = 0; i < returned.length(); i++) {
studentObject = returned.getJSONObject(i);
regi = studentObject.getString("REGISTRATION_NO");
if (regi == reg) {
name = studentObject.getString("STUDENT_NAME");
break;
}
}

android spell checking

I am trying to check the spelling of a word in code using the api's for ics.
I have used the spell checker sample as a starting point and using this I can get a list of suggested words based on the query word, however I can't see how just to check that the word is spelled correctly.
I have the code below from the example and using this I can see and check the suggestions but not the original word.
#Override
public void onGetSuggestions(final SuggestionsInfo[] arg0) {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < arg0.length; ++i) {
// Returned suggestions are contained in SuggestionsInfo
final int len = arg0[i].getSuggestionsCount();
sb.append('\n');
for (int j = 0; j < len; ++j) {
sb.append("," + arg0[i].getSuggestionAt(j));
}
sb.append(" (" + len + ")");
}
runOnUiThread(new Runnable(){
#Override
public void run(){
if(arg0[0].getSuggestionsAttributes()==SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY){
mMainView.append(sb.toString());
}
}
});
}
I have added the if statement that checks for RESULT_ATTR_IN_THE_DICTIONARY but I don't know if this is checking the original word or the first suggestion. (If I enter 'ton' as a query I get 'ten' returned however if I enter 'twn' as a query I get no words returned)
What I really need is for either a correct word or empty string to be returned to a query.
Any help would be appreciated,
Thanks
for (int j = 0; j < len; ++j) {
if(arg0[i].getSuggestionAt(j).toString().contains(string)){
sb.append("," + arg0[i].getSuggestionAt(j));
Log.e("check if", ""+arg0[i].getSuggestionAt(j));
}else{
sb.append("" );
Log.e("check", ""+arg0[i].getSuggestionAt(j));
}
}
Try with this code
and also remove
if(arg0[0].getSuggestionsAttributes()==SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY){
mMainView.append(sb.toString());
}
if condition just put mMainView.append(sb.toString());
hope it's useful to you.

Categories

Resources