问题
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.
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.
Answer from another question SO
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);
}
}
At the method onResume()
I call something like this.
getVersionName gTV = new getVersionName(getApplicationContext());
gTV.execute();
回答1:
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.json
Where 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
Answer Updated JsonFile on your server (Example:www.oye.com/update.json)
{ "name":"AppName", "size":"8mb", "url":"https://www.youall.com/update.apk", "version":"1.08" }
Using 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
}
}});
回答2:
You should set the latest version name of your app on the domain, e.g 1.0.0, instead of simply uploading the whole apk. Then using PackageManager
retrieve the versionName and compare it with the online version.
PackageManager packageManager = getPackageManager();
try {
PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
String localVersionName = packageInfo.versionName;
//Compare with the online version
if(localVersionName != onlineVersionName){
//Update the app
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
来源:https://stackoverflow.com/questions/55587654/how-to-check-from-a-web-if-the-versionname-of-apk-with-the-installed-apk-at-devi