应用程序更新后未显示EULA对话框

时间:2019-10-01 12:11:06

标签: java android dialog eula

EULA对话框正确显示,直到有应用程序更新为止。如果用户已经接受了EULA对话框,则更新后不应再次显示该对话框。

public class AppEULA {
    private String EULA_PREFIX = "appeula";
    private Activity mContext;

    private static final String TAG = MainActivity.class.getSimpleName();

    public AppEULA(Activity context) {
        mContext = context;
    }

    private PackageInfo getPackageInfo() {
        PackageInfo info = null;
        try {
            info = mContext.getPackageManager().getPackageInfo(
                    mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return info;
    }

    public void show() {
        PackageInfo versionInfo = null;
        try {
            versionInfo = mContext.getPackageManager()
                    .getPackageInfo(mContext.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        // The eulaKey changes every time you increment the version number in
        // the AndroidManifest.xml
        final String eulaKey = EULA_PREFIX + versionInfo.versionCode;
        final SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(mContext);

        boolean bAlreadyAccepted = prefs.getBoolean(eulaKey, false);
        if (bAlreadyAccepted == false) {
         ....
        show dialog


 .setPositiveButton(R.string.accept,
                            new Dialog.OnClickListener() {

                                @Override
                                public void onClick(
                                        DialogInterface dialogInterface, int i) {
                                    // Mark this version as read.
                                    SharedPreferences.Editor editor = prefs
                                            .edit();
                                    editor.putBoolean(eulaKey, true);
                                    editor.commit();

                                    // Close dialog
                                    dialogInterface.dismiss();

如果用户已经接受EULA对话框,我希望它不会在应用程序更新后显示。

0 个答案:

没有答案