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();
txt
file which I have writed something likev1.0.2
and then I am reading it withHttpURLConnection c = (HttpURLConnection) u.openConnection(); c.setRequestMethod("GET"); c.connect(); InputStream in = c.getInputStream();
Till now it checks which version is there but the problem it is now I do not know how to update the apk from url. – Zahara