Search code examples
javaandroidhttpversioning

How to allow users to check for the latest app version from inside the app?


I want to add a "Check for update" button in apps so that when someone clicks it, it will display a toast message / progress dialog for checking the app's version.

If new version is found the apps will auto download it to the phone and let user to manually install the updated apps.

Or any others method will do as long as it can check for latest version and notify the user to update.


Update: Now you can do this inside your app using https://developer.android.com/guide/playcore/in-app-updates


Solution

  • Google updated play store two months before. This is the solution working right now for me..

    class GetVersionCode extends AsyncTask<Void, String, String> {
    
        @Override
    
        protected String doInBackground(Void... voids) {
    
            String newVersion = null;
    
            try {
                Document document = Jsoup.connect("https://play.google.com/store/apps/details?id=" + MainActivity.this.getPackageName()  + "&hl=en")
                        .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();
                if (document != null) {
                    Elements element = document.getElementsContainingOwnText("Current Version");
                    for (Element ele : element) {
                        if (ele.siblingElements() != null) {
                            Elements sibElemets = ele.siblingElements();
                            for (Element sibElemet : sibElemets) {
                                newVersion = sibElemet.text();
                            }
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return newVersion;
    
        }
    
    
        @Override
    
        protected void onPostExecute(String onlineVersion) {
    
            super.onPostExecute(onlineVersion);
    
            if (onlineVersion != null && !onlineVersion.isEmpty()) {
    
                if (Float.valueOf(currentVersion) < Float.valueOf(onlineVersion)) {
                    //show anything
                }
    
            }
    
            Log.d("update", "Current version " + currentVersion + "playstore version " + onlineVersion);
    
        }
    }
    

    and don't forget to add JSoup library

    dependencies {
    compile 'org.jsoup:jsoup:1.8.3'}
    

    and on Oncreate()

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
        String currentVersion;
        try {
            currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    
        new GetVersionCode().execute();
    
    }
    

    that's it.. Thanks to this link