单击按钮后打开对话框

时间:2014-02-17 17:58:48

标签: java android button dialog

我已经在我的Android应用程序中实现了WhatsNew对话框。但它仅显示用户何时安装并首次打开新版本。

我想创建一个按钮,当它点击时再次打开相同的WhatsNew对话框。我怎么能这样做?我已经创建了按钮,这里是WhatNewScreen活动的代码:

public class WhatsNewScreen {
    private static final String LOG_TAG                 = "WhatsNewScreen";

    private static final String LAST_VERSION_CODE_KEY   = "last_version_code";

    private Activity            mActivity;

    // Constructor memorize the calling Activity ("context")
    public WhatsNewScreen(Activity context) {
        mActivity = context;
    }

    // Show the dialog only if not already shown for this version of the application
    public void show() {
        try {
            // Get the versionCode of the Package, which must be different (incremented) in each release on the market in the AndroidManifest.xml
            final PackageInfo packageInfo = mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), PackageManager.GET_ACTIVITIES);

            final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivity);
            final long lastVersionCode = prefs.getLong(LAST_VERSION_CODE_KEY, 0);

            if (packageInfo.versionCode != lastVersionCode) {
                Log.i(LOG_TAG, "versionCode " + packageInfo.versionCode + "is different from the last known version " + lastVersionCode);

                final String title = mActivity.getString(R.string.app_name) + " v" + packageInfo.versionName;

                final String message = mActivity.getString(R.string.whatsnew);

                // Show the News since last version
                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity)
                        .setTitle(title)
                        .setMessage(message)
                        .setPositiveButton(android.R.string.ok, new Dialog.OnClickListener() {

                            public void onClick(DialogInterface dialogInterface, int i) {
                                // Mark this version as read
                                SharedPreferences.Editor editor = prefs.edit();
                                editor.putLong(LAST_VERSION_CODE_KEY, packageInfo.versionCode);
                                editor.commit();
                                dialogInterface.dismiss();
                            }
                        });
                builder.create().show();
            } else {
                Log.i(LOG_TAG, "versionCode " + packageInfo.versionCode + "is already known");
            }

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

}

我在MainAcitivty中尝试使用它来打开Dialog。请看一下图片:

http://i.stack.imgur.com/8nav1.png

但是,当我打开应用程序并单击按钮时,它会强制关闭。

1 个答案:

答案 0 :(得分:2)

如果您的应用程序的版本代码发生变化,可以显示Diolag(正如我从您共享的代码中看到的那样)。所以它只会显示一次。如果您想通过单击按钮随时显示它,那么您可以稍微更改show方法。见下文:

    public void show(boolean showWithVersionCheck) {
        try {
            // Get the versionCode of the Package, which must be different (incremented) in each release on the market in the AndroidManifest.xml
            final PackageInfo packageInfo = mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), PackageManager.GET_ACTIVITIES);

            final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivity);

            final long lastVersionCode = prefs.getLong(LAST_VERSION_CODE_KEY, 0);

            boolean showDialog = true;
            if(showWithVersionCheck) {
                if(packageInfo.versionCode == lastVersionCode) {
                   showDialog = false; 
                }
            }

            if (showDialog) {
                Log.i(LOG_TAG, "versionCode " + packageInfo.versionCode + "is different from the last known version " + lastVersionCode);

                final String title = mActivity.getString(R.string.app_name) + " v" + packageInfo.versionName;

                final String message = mActivity.getString(R.string.whatsnew);

                // Show the News since last version
                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity)
                        .setTitle(title)
                        .setMessage(message)
                        .setPositiveButton(android.R.string.ok, new Dialog.OnClickListener() {

                            public void onClick(DialogInterface dialogInterface, int i) {
                                // Mark this version as read
                                SharedPreferences.Editor editor = prefs.edit();
                                editor.putLong(LAST_VERSION_CODE_KEY, packageInfo.versionCode);
                                editor.commit();
                                dialogInterface.dismiss();
                            }
                        });
                builder.create().show();
            } else {
                Log.i(LOG_TAG, "versionCode " + packageInfo.versionCode + "is already known");
            }

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

}

并称之为:

Button button = getYourButton();
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        new WhatsNewScreen(someActivityContext).show(false);
        // or show(true) if you want a version check 
        // If you are calling it from your MainActivity you can write MainActivity.this
        // instead of someActivityContext. 
    }
});

编辑:WhatsNewScreen不会从“活动”扩展,您无法使用您共享的图片上的代码启动它。