问题描述
我正在尝试在运行时检查URL是否存在该应用程序的新版本.我已将应用程序部署在在线域中,就像这样的www.test.com/androidapp/app_debug.apk
一样,它会自动下载我的应用程序.我想做的是在此链接中检查该apk的versionName
是否与已安装的apk相同,如果没有,我将显示一个Dialog
,该消息将为我提供新的versionName
信息,并将具有两个按钮Cancel && Update
.我知道如何执行Dialog
,但是我不知道如何实现Url
和我的apk之间的通信.
I am trying to check at runtime if there is a new version of the app from an url.I have deployed the app at the online domain which is something like this www.test.com/androidapp/app_debug.apk
and this automatically downloads my app.What I am trying to do is check in this link if the versionName
of this apk it is the same with the installed one if not then I will show a Dialog
which will give me a message with the new versionName
and will have two buttons Cancel && Update
.I know how to do the Dialog
but I don't know how to achieve this communication between the Url
and my apk.
我从SO
的答案中尝试了一些代码,但到目前为止我还没有.这是我尝试过的链接.
I have tried some code from an answer from SO
but till now not what I am excepting for.This is the link what I have tried.
到目前为止,我尝试的方法取决于@BryanIbrahim的答案
Here is what I tried so far depends from the answer of @BryanIbrahim
在这里,我获得了该应用程序的当前版本.
Here I get the current version of the app.
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()
中,我这样调用.
At the method onResume()
I call something like this.
getVersionName gTV = new getVersionName(getApplicationContext());
gTV.execute();
推荐答案
Jsoup并非出于您的目的.最好将json文件上传到服务器,然后使用okhttp
进行获取.示例:www.oye.com/update.json 您可以在其中设置版本信息,新功能,URL以及其他信息的位置.否则,您将无法使用jsoup来检查应用程序更新
Jsoup is not for the purpose you are using.Better you upload a json file to your server and get it using okhttp
.Example:www.oye.com/update.jsonWhere you may set information for version,What's new,URL,and other info.Otherwise you will have a bad experience using jsoup for checking your app update
答案更新服务器上的JsonFile(示例:www.oye.com/update.json)
Answer UpdatedJsonFile on your server (Example:www.oye.com/update.json)
{ "name":"AppName", "size":"8mb", "url":"https://www.youall.com/update.apk", "version":"1.08" }
使用okhttp
OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder().url("www.oye.com/update.json").build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
public void onResponse(Call call, final Response response)
throws IOException
{
JSONObject obj=new JSONObject(reponse.body().string);
if(obj.getDouble("version")>yourAppvrrsion){
//update avialable
}else{
//not avialable
}
}});
这篇关于如何在Android Studio中通过Web检查与设备上已安装的APK的APK版本名称是否相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!