【问题标题】:Turn off popup dialog once clicked单击后关闭弹出对话框
【发布时间】:2014-01-08 19:39:28
【问题描述】:

我正在使用此code 在我的家庭活动中创建一个弹出对话框,要求用户评价我的应用程序,代码工作正常,但是当用户选择“不,谢谢”选项时,对话框不应再次显示.

但是,当应用重新打开时,我的 homeActivity 会重新加载,其中的所有内容都会重置,所以尽管如此

editor.putBoolean("dontshowagain", true);
editor.commit();

对话框将再次显示,是否可以在重新加载活动时存储布尔值?

public static class AppRater {

    private final int DAYS_UNTIL_PROMPT = 3;
    private final int LAUNCHES_UNTIL_PROMPT = 7;

    public void app_launched(Context mContext) {
        SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
        if (prefs.getBoolean("dontshowagain", false)) { return ; }

        SharedPreferences.Editor editor = prefs.edit();

        // Increment launch counter
        long launch_count = prefs.getLong("launch_count", 0) + 1;
        editor.putLong("launch_count", launch_count);

        // Get date of first launch
        Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0);
        if (date_firstLaunch == 0) {
            date_firstLaunch = System.currentTimeMillis();
            editor.putLong("date_firstlaunch", date_firstLaunch);
        }

                    // i just use this to test the dialog instantly
            showRateDialog(mContext, editor);



        // Wait at least n days before opening
        if (launch_count >= LAUNCHES_UNTIL_PROMPT) {
            if (System.currentTimeMillis() >= date_firstLaunch + 
                    (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)) {
                showRateDialog(mContext, editor);
            }
        }

        editor.commit();
    }   

    public static void showRateDialog(final Context mContext, final SharedPreferences.Editor editor) {
        final Dialog dialog = new Dialog(mContext);
        dialog.setTitle("Rate MyApp");

        LinearLayout ll = new LinearLayout(mContext);
        ll.setOrientation(LinearLayout.VERTICAL);

        TextView tv = new TextView(mContext);
        tv.setText("We see that you have been using MyApp well. Would you like to rate us?");
        tv.setWidth(240);
        tv.setPadding(4, 0, 4, 10);
        ll.addView(tv);

        Button b1 = new Button(mContext);
        b1.setText("Rate MyApp");
        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Uri uri = Uri.parse("market://details?id=" + mContext.getPackageName());
                Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
                try {
                  mContext.startActivity(goToMarket); // playstore installed
                } catch (ActivityNotFoundException e) { // open website if not
                    mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + mContext.getPackageName())));
                }
                dialog.dismiss();
            }
        });        
        ll.addView(b1);

        Button b2 = new Button(mContext);
        b2.setText("Remind me later");
        b2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        ll.addView(b2);


        Button b3 = new Button(mContext);
        b3.setText("No, thanks");
        b3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                rated = true;
                if (editor != null) {
                    editor.putBoolean("dontshowagain", true);
                    editor.commit();
                }
                dialog.dismiss();
            }
        });
        ll.addView(b3);

        dialog.setContentView(ll);        
        dialog.show();    
    }
}
// http://www.androidsnippets.com/prompt-engaged-users-to-rate-your-app-in-the-android-market-appirater

【问题讨论】:

    标签: android dialog rate


    【解决方案1】:

    很好的问题。我认为原因是,您在方法app_launched 的末尾提交editor,并且对话框中的按钮按下发生在一段时间后,所以当您这样做editor.putBoolean("dontshowagain", true) 时,editor 已经提交因此您的输入不会保存在首选项中。

    如果您需要有关如何更改代码来解决此问题的帮助,请在评论中告诉我。

    编辑 - 一些代码

    首先,不要将编辑器传递给您的showRateDialog 方法,因此将方法签名更改为:

    public static void showRateDialog(final Context mContext)
    

    其次,在您的 onClick 方法中,创建一个新的编辑器,写入标志并提交。

    public void onClick(View v) {
        rated = true;
        SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
        // create editor, write stuff and commit, all in one line.
        prefs.edit().putBoolean("dontshowagain", true).commit();
        dialog.dismiss();
    }
    

    【讨论】:

    • 是的,请,事实上我已经尝试设置另一个布尔值并测试该值是否为假,然后我不会调用 showRateDialog(),但它仍然不起作用
    • 谢谢,我已经按照你的建议修改了我的代码,但是在我退出应用程序并再次使用它后,对话框仍然加载,顺便说一句,我在 homeActivity AppRater.showRateDialog(this); 的 onCreate 方法中调用了这个类
    • 对话框出现后,您点击“不,谢谢”,再次启动应用程序时,对话框仍然显示?
    • 对不起,这是我的错,我应该打电话给AppRater.app_launched(this),但现在对话框甚至都打不开了
    【解决方案2】:

    可笑的是正确的。请改用此代码并打开一个新编辑器:

    b3.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            rated = true;
            SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
            SharedPreferences.Editor editorNew = prefs.edit();
            editorNew.putBoolean("dontshowagain", true);
            editorNew.commit();
    
            dialog.dismiss();
        }
    });
    

    然后通过删除编辑器参数来清理showRateDialog。请记住,侦听器的代码在设置它们的方法返回很久之后才被调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-16
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多