0

Unable to see the in-app prompt in Android version 11 and above.

I have used the in-app to see the prompt in the app. But it failed to show the prompt in android versions 11 and 12. I have added the below code in my fragment.

Note: If I add it in the Activity file same error is occurring in Android version 11 and 12.

Restarting the app after update also not happening in the version 11 10 and 12

Code:

 @Override
    public void onCreate(Bundle savedInstanceState) {
       Calendar calendar = Calendar.getInstance();
        int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
        SharedPreferences settings = getActivity().getSharedPreferences("PREFS", 0);
        int lastday = settings.getInt("day", 0);
        if (lastday != currentDay) {
            SharedPreferences.Editor editor = settings.edit();
            editor.putInt("day", currentDay);
            editor.commit();
            appUpdateManager = AppUpdateManagerFactory.create(getContext());
            installStateUpdatedListener = state -> {
                if (state.installStatus() == InstallStatus.INSTALLED) {
                    removeInstallStateUpdateListener();
                } else {
                    LogUtils.LOGD("state.installstatus");
                    Toast.makeText(getContext(), "App update is in progress! App will restart soon.", Toast.LENGTH_LONG).show();
                }
            };
            appUpdateManager.registerListener(installStateUpdatedListener);
            checkUpdate();
        }

  }





private void checkUpdate() {
        Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
        appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
                startUpdateFlow(appUpdateInfo);
            } else if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_NOT_AVAILABLE) {
                Log.e("Already updated", "the app and no new version available.");
            } else if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
                popupSnackBarForCompleteUpdate();
            }
        });
    }

    private void startUpdateFlow(AppUpdateInfo appUpdateInfo) {
        try {
            appUpdateManager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.FLEXIBLE, getActivity(), FLEXIBLE_APP_UPDATE_REQ_CODE);
        } catch (IntentSender.SendIntentException e) {
            e.printStackTrace();
        }
    }

    private void popupSnackBarForCompleteUpdate() {
        Toast.makeText(getContext(), "App is downloaded successfully.", Toast.LENGTH_LONG).show();
        PackageManager packageManager = getContext().getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(getContext().getPackageName());
        ComponentName componentName = intent.getComponent();
        Intent mainIntent = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            mainIntent = Intent.makeRestartActivityTask(componentName);
        }
        // mainIntent = Intent.makeRestartActivityTask(componentName);
        getContext().startActivity(mainIntent);
        Runtime.getRuntime().exit(0);

    }


  private void removeInstallStateUpdateListener() {
        if (appUpdateManager != null) {
            appUpdateManager.unregisterListener(installStateUpdatedListener);
        }
    }

 @Override
    public void onResume() {
        super.onResume();
  if (appUpdateManager != null) {
                appUpdateManager.getAppUpdateInfo().addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>()
                {
                    @Override
                    public void onSuccess(AppUpdateInfo result)
                    {
                        if(result.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS)
                        {
                            try
                            {
                                appUpdateManager.startUpdateFlowForResult(result,AppUpdateType.IMMEDIATE, getActivity() ,10);
                            } catch (IntentSender.SendIntentException e)
                            {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }


 @Override
    public void onStop() {
        removeInstallStateUpdateListener();
       super.onStop();
    }
}
sejn
  • 2,040
  • 6
  • 28
  • 82

0 Answers0