私は、現在のバージョンコードがサーバ上のコードよりも5バージョン以上遅れているかどうかをチェックしたいと思う。intが他のintより5小さいかどうかをチェックする方法?
現在のバージョンコードが100
で、サーバー上のバージョンコードが105
以上の場合は、「アプリは廃止」と表示されます。しかし、問題は、それは常にこのトーストを示して、私のサーバー上のコードは104
です。ここ
if (updateVersionCode >= info.versionCode - 5) {
Toast.makeText(context, "force update, app is 5 versions behind!", Toast.LENGTH_LONG)
.show();
} else {
//not obsolete
}
はgeeks-
public static void compareVersions(Context context) {
// 1) get local apk version code
PackageManager manager = context.getPackageManager();
PackageInfo info = null;
try {
info = manager.getPackageInfo(context.getPackageName(), 0);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 2) get version code from downloaded .txt file
int updateVersionCode = info.versionCode;
try {
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, "ver.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
if ((line = br.readLine()) != null) {
updateVersionCode = Integer.parseInt(line);
}
br.close();
} catch (Exception e) {
// Handle errors!
}
// 3) compare Versions
if (updateVersionCode > info.versionCode) {
// download apk file
context.startService(new Intent(context,
NSOMUHDownloadApkService.class));
Toast.makeText(context, "updates are available", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(context, "up to date!", Toast.LENGTH_LONG).show();
}
if (updateVersionCode >= info.versionCode - 5) {
Toast.makeText(context, "force update, app is 5 versions behind!", Toast.LENGTH_LONG)
.show();
} else {
//not obsolete
}
}
PSのための私の完全なコードです:それは.txtファイルにサーバーからバージョンをダウンロードし、それを読み込みます。
あなたは私はあなたがポップスターと私は間違って持って怖い 'BuildConfig.VERSION_CODE' –