【发布时间】:2019-08-30 10:37:23
【问题描述】:
我正在尝试在运行时检查是否有来自 url 的应用程序的新版本。
我已经在类似www.test.com/androidapp/app_debug.apk 的在线域中部署了该应用程序,这会自动下载我的应用程序。
我要做的是检查此链接,如果此 apk 的 versionName 与已安装的相同,否则我将显示 Dialog,它将给我一条带有新 versionName 的消息和将有两个按钮Cancel && Update。
我知道如何进行Dialog,但我不知道如何在Url 和我的apk 之间实现这种通信。
我已经从SO 的答案中尝试了一些代码,但直到现在还不是我想要的。
这是我尝试过的链接。
Answer from another question SO
这是我到目前为止所尝试的取决于@BryanIbrahim 的回答
我在这里获取应用程序的当前版本。
String getVersionFromUrl = "http://test.com/AndroidApp/text.txt";
//At text.txt I have only test.v1.0.2
URL u = null;
try {
u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.connect();
InputStream in = c.getInputStream();
final ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer); // Read from Buffer.
bo.write(buffer); // Write Into Buffer.
String getVersion = bo.toString().substring(12, 17);
String getVersionFromUrl = BuildConfig.VERSION_NAME;
if (!getVersionFromUrl.equals(getVersion)) {
runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("It is a new version of this app");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder1.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder1.show();
}
});
}
} catch (Exception e) {
Log.e("YourApp", "Well that didn't work out so well...");
Log.e("YourApp", e.getMessage());
}
return path;
}
@Override
protected void onPostExecute(String path) {
Intent i = new Intent();
String path2 = "http://test.com/AndroidApp/testv1.0.2.apk";
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(path2)), "application/vnd.android.package-archive" );
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// mContext.startActivity(i);
}
}
在 onResume() 方法中,我调用了这样的方法。
getVersionName gTV = new getVersionName(getApplicationContext());
gTV.execute();
【问题讨论】:
标签: android jsoup androidhttpclient