We can check Update by Add these Code:
First we need to add dependencies :
implementation 'org.jsoup:jsoup:1.10.2'
Second we need to create Java File:
import android.app.Activity;
import android.app.AlertDialog;
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.widget.Toast;
import org.jsoup.Jsoup;
public class CurrentVersion{
private Activity activity;
public CurrentVersion(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.setCancelable(false);
builder.show();
}
else {
if (manualCheck) {
Toast.makeText(activity, "No Update Available", Toast.LENGTH_SHORT).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();
}
}
Third We need to add this class in your main class where you want to show the update:
AppUpdateChecker appUpdateChecker=new AppUpdateChecker(this);
appUpdateChecker.checkForUpdate(false);
I hope it will help you