It might be useful for someone else. I tried this way
First create a class having couple of methods to launch play store and get app version code and version information this way
public class CheckForUpdate {
public static final String ACTION_APP_VERSION_CHECK="app-version-check";
public static void launchPlayStoreApp(Context context)
{
final String appPackageName = context.getPackageName(); // getPackageName() from Context or Activity object
try {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
public static int getRemoteVersionNumber(Context context)
{
int versionCode=0;
try {
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
String version = pInfo.versionName;
versionCode=pInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
}
Second create another util class having sharedpreference methods to save and retrieve version code this way
public class PreferenceUtils {
// this is for version code
private final String APP_VERSION_CODE = "APP_VERSION_CODE";
private SharedPreferences sharedPreferencesAppVersionCode;
private SharedPreferences.Editor editorAppVersionCode;
private static Context mContext;
public PreferenceUtils(Context context)
{
this.mContext=context;
// this is for app versioncode
sharedPreferencesAppVersionCode=mContext.getSharedPreferences(APP_VERSION_CODE,MODE_PRIVATE);
editorAppVersionCode=sharedPreferencesAppVersionCode.edit();
}
public void createAppVersionCode(int versionCode) {
editorAppVersionCode.putInt(APP_VERSION_CODE, versionCode);
editorAppVersionCode.apply();
}
public int getAppVersionCode()
{
return sharedPreferencesAppVersionCode.getInt(APP_VERSION_CODE,0); // as default version code is 0
}
}
Finally you can use in your launcher activity or any other activity from where you want show alert dialog box to user to update app if its updated.
public class DashboardActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...........
//check whether app is first time launched
AppLaunchChecker.onActivityCreate(this);
alertAppUpdate();
}
Implement alertAppUpdate() method this way
private void alertAppUpdate()
{
int remoteVersionCode=CheckForUpdate.getRemoteVersionNumber(this);
PreferenceUtils preferenceUtils=new PreferenceUtils(this);
if(AppLaunchChecker.hasStartedFromLauncher(this))
{
preferenceUtils.createAppVersionCode(remoteVersionCode);
Log.i("First time","First time app is launched");
}
int existingVersionCode= preferenceUtils.getAppVersionCode();
if(remoteVersionCode>existingVersionCode)
{
/*
**
* app is updated, alert user to update app from playstore
* if app is updated then only save the version code in preferenceUtils
*
*/
AlertDialog.Builder dialogBuilder=AlertDialogBox.getAlertDialogBuilder(this,"Update available","Do you want to update your app now?");
dialogBuilder.setPositiveButton("Update Now", (dialogInterface, i) -> {
CheckForUpdate.launchPlayStoreApp(this);
Log.i("app update service","app is needed to update");
preferenceUtils.createAppVersionCode(remoteVersionCode);
});
dialogBuilder.setNegativeButton("Later",(dialogInterface,i)->{
});
dialogBuilder.show();
}
}
If any errors just let me know . Thank you.