Prompt Android App User to Update App if current version <> market version

后端 未结 7 753
温柔的废话
温柔的废话 2020-12-01 02:46

Lets say my Android App version 0.1 is installed currently on the User\'s phone. Everytime they launch my App I want to check if there is a different version available in th

相关标签:
7条回答
  • 2020-12-01 03:50

    You can use this Android Library: https://github.com/danielemaddaluno/Android-Update-Checker. It aims to provide a reusable instrument to check asynchronously if exists any newer released update of your app on the Store. It is based on the use of Jsoup (http://jsoup.org/) to test if a new update really exists parsing the app page on the Google Play Store:

    private boolean web_update(){
        try {       
            String curVersion = applicationContext.getPackageManager().getPackageInfo(package_name, 0).versionName; 
            String newVersion = curVersion;
            newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + package_name + "&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()
                    .select("div[itemprop=softwareVersion]")
                    .first()
                    .ownText();
            return (value(curVersion) < value(newVersion)) ? true : false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    

    And as "value" function the following (works if values are beetween 0-99):

    private long value(String string) {
        string = string.trim();
        if( string.contains( "." )){ 
            final int index = string.lastIndexOf( "." );
            return value( string.substring( 0, index ))* 100 + value( string.substring( index + 1 )); 
        }
        else {
            return Long.valueOf( string ); 
        }
    }
    

    If you want only to verify a mismatch beetween versions, you can change:

    "value(curVersion) < value(newVersion)" with "value(curVersion) != value(newVersion)"

    0 讨论(0)
提交回复
热议问题