简体   繁体   中英

android - comparing string with boolean in java

I have an app for android.

The app has a self-checking method to be sure the it's updated to the last version available.

Unfortunatly, during my last release I wanted to make this method being able to check if the version is lower than the last one available, so that it pop up the update message only in case the version found on the device is lower than the actual one.

But I made a mistaked in my code and published my app, because of this mistake I can t get my users to update my app.

Here is the code I should have used:

 if (Integer.parseInt(lastVersion.replace(".", "")) > Integer.parseInt(actualVersion.replace(".", ""))){ //update...}

and here is the code I have used:

 if (lastVersion.equals(Integer.parseInt(lastVersion.replace(".", "")) > Integer.parseInt(actualVersion.replace(".", "")))){ //update...}

As you can see, and I have no idea how I ended doing such a mistake, instead of comparing the 2 versions I am checking the the result of lastVersion>actualVersion is equals to the string lastVersion....

The only thing I can interact on is lastVersion as it is a variable taken from my own api.

I want to find what I could use in my json table to make the condition to work, this means that lastVersion should be equal to a boolean, but it must remain a string containing at least 1 number(because of the parseInt()). And actualVersion is 1.94

I have tried using 0 and 1 without success.

PS: my app isn t on the play store so there is no automatic update.

You cannot fix it because of the reasons others already mentioned. I would also like to point out that your current approach is flawed. With your current code, version 10.1 will equal 1.01 for example.

Instead, do not compare app version names and start comparing app version codes instead. That is, when you release version 1.0, release that version to code 1; when you release version 1.1, release it with code 2 and so on. In your app, compare version codes, and ignore version names if you want to make your life easier. The android manifest uses this mechanism to check app versions on the Play Store. Look at <manifest android:versionCode="integer" android:versionName="string" /> for reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM