Android notify users when app version is updated on play store .

1 . First get current play store version.

int currentVersion,updateVersion;
VersionChecker versionChecker = new VersionChecker();
try {
String latestVersion = versionChecker.execute().get();
updateVersion = Integer.parseInt(latestVersion.substring(latestVersion.length()-1));
Toast.makeText(HomeActivity.this, latestVersion.toString(), Toast.LENGTH_SHORT).show();

} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}


PackageManager manager = this.getPackageManager();
PackageInfo info = null;
try {
info = manager.getPackageInfo(this.getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}

2. Get current app version with PackageManager .

PackageManager manager = this.getPackageManager();
PackageInfo info = null;
try {
info = manager.getPackageInfo(this.getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
// Toast.makeText(this,
// "PackageName = " + info.packageName + "\nVersionCode = "
// + info.versionCode + "\nVersionName = "
// + info.versionName + "\nPermissions = " + info.permissions, Toast.LENGTH_SHORT).show();

currentVersion = info.versionCode;

3. Check if play store version is greater or not .

if (currentVersion<updateVersion){
updateAlert();
}

4. If greater show an alert dialog to download update version .

public void updateAlert(){


AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("There has an updated version on play store , update now ");
alertDialogBuilder.setPositiveButton("Update",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {


String url = "https://play.google.com/store/apps/details?id=coinatmfinder.com.app.android";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);


}
});

alertDialogBuilder.setNegativeButton("Later",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {


dialog.dismiss();


}
});

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}


 

Leave a Reply

Your email address will not be published. Required fields are marked *