Saturday, 15 December 2018

Auto check for update function for android app | android studio

If you are having an app on Google Play Store and you want that whenever you update the app on playstore and when the user opens the older version of app he/she will get a dialog that there is an Update available .
Each time the user will open the app he/she will get the same dialog until he/she updates the app.

Advantages:

This will inform the user as a notice that there is an update.

Users will have the latest version of app.
Automatically checks for update in the background.
There is also an option for mannual check and automatic check.




How to use:

Step-I
I have already create a class(AppUpdateChecker.java) to achieve this feature that will handle it automatically.

AppUpdateChecker.java
 
 package com.programmingdesire;  
 import android.app.Activity;  
 import android.app.ProgressDialog;  
 import android.content.DialogInterface;  
 import android.content.Intent;  
 import android.content.pm.PackageInfo;  
 import android.content.pm.PackageManager;  
 import android.net.Uri;  
 import android.os.AsyncTask;  
 import android.support.v7.app.AlertDialog;  
 import android.widget.Toast;  
 import org.jsoup.Jsoup;  
 public class AppUpdateChecker {  
   private Activity activity;  
   public AppUpdateChecker(Activity activity) {  
     this.activity = activity;  
   }  
   //current version of app installed in the device  
   private String getCurrentVersion(){  
     PackageManager pm = activity.getPackageManager();  
     PackageInfo pInfo = null;  
     try {  
       pInfo = pm.getPackageInfo(activity.getPackageName(),0);  
     } catch (PackageManager.NameNotFoundException e1) {  
       e1.printStackTrace();  
     }  
     return pInfo.versionName;  
   }  
   private class GetLatestVersion extends AsyncTask<String, String, String> {  
     private String latestVersion;  
     private ProgressDialog progressDialog;  
     private boolean manualCheck;  
     GetLatestVersion(boolean manualCheck) {  
       this.manualCheck = manualCheck;  
     }  
     @Override  
     protected void onPostExecute(String s) {  
       super.onPostExecute(s);  
       if (manualCheck)  
       {  
         if (progressDialog!=null)  
         {  
           if (progressDialog.isShowing())  
           {  
             progressDialog.dismiss();  
           }  
         }  
       }  
       String currentVersion = getCurrentVersion();  
       //If the versions are not the same  
       if(!currentVersion.equals(latestVersion)&&latestVersion!=null){  
         final AlertDialog.Builder builder = new AlertDialog.Builder(activity);  
         builder.setTitle("An Update is Available");  
         builder.setMessage("Its better to update now");  
         builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {  
           @Override  
           public void onClick(DialogInterface dialog, int which) {  
             //Click button action  
             activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+activity.getPackageName())));  
             dialog.dismiss();  
           }  
         });  
         builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {  
           @Override  
           public void onClick(DialogInterface dialog, int which) {  
             //Cancel button action  
           }  
         });  
         builder.setCancelable(false);  
         builder.show();  
       }else {  
         if (manualCheck) {  
           Toast.makeText(activity, "No Update Available", Toast.LENGTH_SHORT).show();  
         }  
       }  
     }  
     @Override  
     protected void onPreExecute() {  
       super.onPreExecute();  
       if (manualCheck) {  
         progressDialog=new ProgressDialog(activity);  
         progressDialog.setMessage("Checking For Update.....");  
         progressDialog.setCancelable(false);  
         progressDialog.show();  
       }  
     }  
     @Override  
     protected String doInBackground(String... params) {  
       try {  
         //It retrieves the latest version by scraping the content of current version from play store at runtime  
         latestVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + activity.getPackageName() + "&hl=it")  
             .timeout(30000)  
             .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")  
             .referrer("http://www.google.com")  
             .get()  
             .select(".hAyfc .htlgb")  
             .get(7)  
             .ownText();  
         return latestVersion;  
       } catch (Exception e) {  
         return latestVersion;  
       }  
     }  
   }  
   public void checkForUpdate(boolean manualCheck)  
   {  
     new GetLatestVersion(manualCheck).execute();  
   }  
 }  

Step-II
Add a library to your build.gradle(App level) file.

   implementation 'org.jsoup:jsoup:1.10.2'  

Step-III
The last step is to copy that class and then write two lines of code.


 AppUpdateChecker appUpdateChecker=new AppUpdateChecker(this);  //pass the activity in constructure
 appUpdateChecker.checkForUpdate(false); //mannual check false here

Lets understand what is manual check

If manual check is true then
Manual check will be on a button click this will show the dialog as "Checking for update"



If manual  check is false then
Checking for update will be performed in the background and finally a dialog will be visible if there is any update.


If user will click on update it will open your app in playstore to update your app.

Thats all.

Thanks for visiting, I will come with new code snippet.
Happy Coding.......





7 comments:

  1. I impressed by the quality of information on this website. I am very enjoyed for this blog. Its an informative topic. Very useful info. I am sure I will visit this place again soon. Hope to see more posts soon! Android Applications

    ReplyDelete
  2. hello
    thanks for the code i just use it and it's work good on my emulator the popup is showed
    but i have a Question :
    if the user clicked on update now the he update the did the pop up will show again or not ?

    ReplyDelete
  3. Sorry for the late reply .....
    Once user updates the app and if there is no update and when you click on update now it will show a message that there is no update

    ReplyDelete
  4. This was rather a harsh rule, and kept all the auto repair companies unless they were at gas stations from operating on any main streets. used car dealerships

    ReplyDelete
  5. i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful. auto repair Freehold NJ

    ReplyDelete
  6. I get an error when I click the update button. Help plz

    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://details?id=com.gdglkdg.dgadg

    ReplyDelete

WhatsApp will give Rs 1.8 crores to new Indian startups, Here is all you need to know

The "Startup India WhatsApp Grand Challenge" has been appraise growth in the entrepreneurial sector. Total prize money of Rs 1.8...