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();
TheCoderGuy
  • 771
  • 6
  • 24
  • 51

2 Answers2

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();
    }
Oush
  • 3,090
  • 23
  • 22
  • How to get from the domain the `versionName` how it is that possible? You have only added how to get the `localVersionName` ? – TheCoderGuy Apr 09 '19 at 08:39
  • It would be more convenient if you had a database to set and retrieve. You could use https://jsoup.org/, a java HTML parser to extract the exact version name from an element – Oush Apr 09 '19 at 08:42
  • 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 ? – TheCoderGuy Apr 09 '19 at 08:47
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
                         }


}});
nhCoder
  • 451
  • 5
  • 11
  • 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. – TheCoderGuy Apr 10 '19 at 14:18
  • Is your app available at play/app store – nhCoder Apr 10 '19 at 15:09
  • 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. – TheCoderGuy Apr 10 '19 at 15:11
  • I have updated the question please see for changes. – TheCoderGuy Apr 10 '19 at 16:01
  • How to make that update available to be installed ? – TheCoderGuy Apr 11 '19 at 06:25