Querying latest application version from the Android Market

后端 未结 4 747
無奈伤痛
無奈伤痛 2021-01-07 07:15

Can I query the Android Market for the latest version of my application in code? I would like to show an update notification for the user when a new version is available.

4条回答
  •  没有蜡笔的小新
    2021-01-07 07:34

    I bumped into the same problem here. So I thought... why not use AppBrain.

    I wrote a small function that gets your latest app version from the AppBrain website.

    public String getLatestVersionNumber()
    {
        String versionNumber = "0.0.0";
    
        try
        {
            Document doc = Jsoup.connect("http://www.appbrain.com/app/wallpaper-switch/com.mlevit.wallpaperswitch").get();
            Elements changeLog = doc.select("div.clDesc");
    
            for (Element div : changeLog)
            {
                String divText = div.text();
    
                if (divText.contains("Version"))
                {
                    return divText.split(" ")[1];
                }
    
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    
        return versionNumber;
    }
    

    I use the jsoup Java HTML Parser to parse the HTML and from there on it's pretty simple.

    Once you've retrieved it, since it's a String the best way I can think of to compare two versions together is to remove the full stops (.) that way your version number would go from say 1.1.2 to 112 then it's just a simple matter of comparing two Integers.

提交回复
热议问题