How to check from a web if the versionName of apk with the installed apk at device it is the same or not in Android Studio
Asked Answered
Z

2

0

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();
Zahara answered 9/4, 2019 at 7:51 Comment(0)
C
0

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
                         }


}});
Concubine answered 10/4, 2019 at 13:35 Comment(5)
I have created there a txt file which I have writed something like v1.0.2 and then I am reading it with HttpURLConnection 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
Is your app available at play/app storeConcubine
Nope, I am trying at first some bugs to fix and to see if the update it is possible from the Web and then I will upload to the playstore.Zahara
I have updated the question please see for changes.Zahara
How to make that update available to be installed ?Zahara
D
0

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();
    }
Dislodge answered 9/4, 2019 at 8:32 Comment(3)
How to get from the domain the versionName how it is that possible? You have only added how to get the localVersionName ?Zahara
It would be more convenient if you had a database to set and retrieve. You could use jsoup.org, a java HTML parser to extract the exact version name from an elementDislodge
I am going to try that with jsoup.org and you said it would be more convenient if I had a database to set and retrieve what do you mean exactly ?Zahara
C
0

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
                         }


}});
Concubine answered 10/4, 2019 at 13:35 Comment(5)
I have created there a txt file which I have writed something like v1.0.2 and then I am reading it with HttpURLConnection 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
Is your app available at play/app storeConcubine
Nope, I am trying at first some bugs to fix and to see if the update it is possible from the Web and then I will upload to the playstore.Zahara
I have updated the question please see for changes.Zahara
How to make that update available to be installed ?Zahara

© 2022 - 2024 — McMap. All rights reserved.