Question Detail

How to check programmatically check Play Store for app updated Version is live android?

5 years ago Views 8411 Visit Post Reply

I have updated my Android App on Play Store. Sometimes I change in my app which is totally diffrent from the last application so when old app user use the app it got crashed. 
I want to add a AlertBox in my app for show "Your App is Live now Please Update It". But i am not able to fine any APIs which help me to get Live Android App Version Code in my coding to perform task on AlertBox Accept button.

if there is a critical update and the user has not updated yet, he cannot use the app until he does so.

Is this possible using the Play Store?


Thread Reply

Hemant Sharma

- 5 years ago

Import Dependencies{

compile 'org.jsoup:jsoup:1.11.3'

}

UrlResponce.java

//this class will help us to get Response from AsynkTask Class

public abstract class UrlResponce {
    public abstract void onReceived(String resposeStr);

}

 

MainActivity.java Add below code in OnCreate function 

String currentVersion = ""
try {
    currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}
new CheckIsUpdateReady("https://play.google.com/store/apps/details?id=" + getPackageName() + "&hl=en", new UrlResponce() {
    @Override
    public void onReceived(String resposeStr) {
        Toast.makeText(getBaseContext(),resposeStr,Toast.LENGTH_SHORT).show();
    }
}).execute();

 

CheckIsUpdateReady.java

//This class will check what is your online Android Application's Version

import android.os.AsyncTask;
import android.util.Log;
import com.techximum.funbooks.JsonParser.UrlResponce;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class CheckIsUpdateReady extends AsyncTask<Void, String, String> {
    String appURL="";
    private UrlResponce mUrlResponce;
    public CheckIsUpdateReady(String appURL, UrlResponce callback) {
        this.appURL=appURL;
        mUrlResponce = callback;
    }

    @Override

    protected String doInBackground(Void... voids) {

        String newVersion = null;

        try {
            Document document = Jsoup.connect(appURL)
                    .timeout(20000)
                    .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()) {
            mUrlResponce.onReceived(onlineVersion);
        }

        Log.d("update", " playstore App version " + onlineVersion);

    }
}

Hemant Sharma

- 5 years ago

Import Dependencies{

compile 'org.jsoup:jsoup:1.11.3'

}

UrlResponce.java

//this class will help us to get Response from AsynkTask Class

public abstract class UrlResponce {
    public abstract void onReceived(String resposeStr);

}

 

MainActivity.java Add below code in OnCreate function 

String currentVersion = ""
try {
    currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}
new CheckIsUpdateReady("https://play.google.com/store/apps/details?id=" + getPackageName() + "&hl=en", new UrlResponce() {
    @Override
    public void onReceived(String resposeStr) {
        Toast.makeText(getBaseContext(),resposeStr,Toast.LENGTH_SHORT).show();
    }
}).execute();

 

CheckIsUpdateReady.java

//This class will check what is your online Android Application's Version

import android.os.AsyncTask;
import android.util.Log;
import com.techximum.funbooks.JsonParser.UrlResponce;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class CheckIsUpdateReady extends AsyncTask<Void, String, String> {
    String appURL="";
    private UrlResponce mUrlResponce;
    public CheckIsUpdateReady(String appURL, UrlResponce callback) {
        this.appURL=appURL;
        mUrlResponce = callback;
    }

    @Override

    protected String doInBackground(Void... voids) {

        String newVersion = null;

        try {
            Document document = Jsoup.connect(appURL)
                    .timeout(20000)
                    .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()) {
            mUrlResponce.onReceived(onlineVersion);
        }

        Log.d("update", " playstore App version " + onlineVersion);

    }
}