是否可以在启动 Android 应用程序时检查用户是否拥有最新版本,如果没有则强制他更新?或者至少检查一下是否有新版本并告诉他,该应用只有在他下载并安装更新后才能再次运行?
背景是我有一个需要与服务器通信的应用程序。当我更改服务器和客户端之间的接口(interface)中的某些内容时,我想确保客户端具有最新版本。
有没有本地方法可以做到这一点?还是我必须自己检查?
只有在你有 mysql 数据库的情况下才能访问?是否可以仅使用网页上的纯文本来执行此操作,并使用当前的应用版本进行检查?
最佳答案
is it possible to do this with just plain text on a webpage and have it checked with the current app version?
这就是我会做的:
在文本文件中检查服务器上的最新版本:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
String latestVersion = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
latestVersion = str.toString();
然后将其与安装的版本进行比较:
private String getSoftwareVersion() {
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
return packageInfo.versionName;//EDIT: versionCode would be better
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Package name not found", e);
};
}
关于java - 强制用户拥有最新的 Android 应用程序版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6619972/