Reputation: 105
I am developing an application which checks if an update is available. The problem is that my application can't recognize itself being in the latest version.
I have a file on my server, and the file contains the latest version number (1.0.1). My application version is also 1.0.1 but when I check the updates my application indicates that an update is available, the text says that the latest and the installed version is 1.0.1.
So here is my code:
(checkupdatebutton)
{
setContentView(R.layout.updater);
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httppost = new HttpGet("http://myserver.com/latestversion.txt");
HttpResponse response;
response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(is,"UTF-8"));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line + "\n");
}
TextView versionupdnew = (TextView)findViewById(R.id.versionupdnew);
//Set text to value of my file (latestversion.txt)
versionupdnew.setText(total);
TextView installedversion = (TextView)findViewById(R.id.versionupdcur);
installedversion.setText(R.string.version);
TextView title = (TextView)findViewById(R.id.title);
if(installedversion.equals(versionupdnew))
{
//Current version is latest
title.setText("No update needed!");
Button buttonUpdate = (Button)findViewById(R.id.buttonUpdate);
buttonUpdate.setEnabled(false);
}else{
title.setText("New version is available!");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Upvotes: 3
Views: 336
Reputation: 1236
For you current problem, you are comparing TextViews rather than strings, so comparing strings will help.
Though I will really suggest you alternative approach,
-You can setup a small API on server say "check_version"
-You will pass the current version number to API like:"www.myServer.com/check_version.php?v=1.0.1"
-The API will return some JSON saying
{
status:"UPDATE_AVAILABLE",
message: "An update is available",
url: "http://play.google.com?xxxyyyzzz"
}
Here you can use the "status" field to determine if update is available.. it can also inform if update is critical or not.. like you can have possible values
"UPDATE_AVAILABLE"
"UPDATE_REQUIRED"
"LATEST".
While message and URL can be blank if its "LATEST". All comparison logic will be on server.
Upvotes: 0
Reputation: 7915
Use SharedPreferences to store the latest version, otherwise using R.string.version will always check against the version that came with installed apk, and when a new version is downloaded, it won't update this.
It would be simplest if you mapped your versions to integers, that way you won't need to parse the version string. Each new version would be 1 greater than the last one.
Get the current version:
// use preferences to get the current version, with default = 1
// (replace 1 with the version that came with the apk)
preferences.getInt("current_version", 1);
Get the version from your server.
Check if server version is greater than current version, and update if necessary:
if (server_version > current_version) {
update();
preferences_editor.putInt("current_version", server_version).commit();
}
Now the next time you check to see if an update is needed, you will be checking against the most recently updated version.
Upvotes: 2
Reputation: 4810
TextView.equals()
does not return true when the text values of the two boxes are equal. You will want to compare the text values of the two boxes.
For example:
if(installedversion.getText().toString().equals(versionupdnew.getText().toString()))
Note that the TextView.getText()
function does not return a String
(the API documentation states that it returns a CharSequence
, however if it is set to use Spannable
or Editable
it can be cast to a different type. using toString()
on these types returns the text value of the box as a String
and in my experience is the most reliable way to compare the values of two View
s)
Upvotes: 2