如何在应用程序中的三天内显示一次对话框

时间:2016-10-27 18:04:35

标签: java android sharedpreferences

这是我正在运行的代码请告诉我代码按钮"不,谢谢"如果用户点击此按钮,则对话框永远不会显示

 public class MainActivity extends Activity {


    Button btnRegId;
    EditText etRegId;
    String regID;

    GoogleCloudMessaging gcm;
    String regid,url;

    //String PROJECT_NUMBER = "90787073097";
    String PROJECT_NUMBER =  "440085976573";


    String android_id,version,ver;


ImageView mega4,todayTips,latstnews,sportquiz,tipister;

TextView txtname;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  // etRegId = (EditText) findViewById(R.id.edtvID);




    //********************For Rating APP **********************
     SharedPreferences sharedPrefs = MainActivity.this.getSharedPreferences("RATER", 0);

     SharedPreferences.Editor prefsEditor = sharedPrefs.edit();

    long time = sharedPrefs.getLong("displayedTime", 0);
    if (time < System.currentTimeMillis() - 259200000) {
       displayDialog();
       prefsEditor.putLong("displayedTime", System.currentTimeMillis()).commit();
    }
 }

   //dialog box Function for rating app.

private void displayDialog() {
    // TODO Auto-generated method stub
      DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
            case DialogInterface.BUTTON_POSITIVE:
                //Yes button clicked
                   Intent in = new Intent(android.content.Intent.ACTION_VIEW);
                   in.setData(Uri.parse(url));
                   startActivity(in);
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                //No button clicked
                break;
            }
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Rate This App");
    builder.setMessage("You really seem to like this app, "
              +"since you have already used it %totalLaunchCount% times! "
              +"It would be great if you took a moment to rate it.")
    .setPositiveButton("Rate Now", dialogClickListener)
        .setNegativeButton("Latter", dialogClickListener)
        .setNeutralButton("No,thanks", dialogClickListener).show();

   }
   //End dialog box Function for rating app.
  }

这是我的代码实际上我想在应用程序中实现应该在三天内显示一次的应用程序评级对话框

2 个答案:

答案 0 :(得分:3)

这是我10天的函数代码。 我将初始值设置为当前时间-11天以确保它将在首次启动应用程序时运行

  public void dialogEvery10Days() {
  Long duration = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getLong("duration", System.currentTimeMillis()-TimeUnit.DAYS.toMillis(11));
      if (System.currentTimeMillis()-duration > TimeUnit.DAYS.toMillis(10)) {
            // inflateDialog is a function containing the functionality of popping up the dialog
            Dialog dialog = inflateDialog(R.layout.dialog_layout);

            getSharedPreferences("PREFERENCE", MODE_PRIVATE)
                    .edit()
                    .putLong("duration", System.currentTimeMillis())
                    .apply();
        }
    }

答案 1 :(得分:2)

您必须像这样初始化SharedPreferences和Editor对象:

   SharedPreferences prefs = mContext.getSharedPreferences("RATER", 0);
   SharedPreferences.Editor editor = prefs.edit();

<强>更新

当用户cliks时,只需保存一个布尔值,谢谢并在显示对话框之前检查它。如果为true则不会显示对话框。

//Saving a boolean on no thanks button click

SharedPreferences prefs = mContext.getSharedPreferences("RATER", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("NO THANKS", true));
editor.apply();

在对话框显示方法中访问它。

    SharedPreferences prefs = mContext.getSharedPreferences("RATER", 0);
    if (prefs.getBoolean("NO THANKS", false)) {
        return;
    }else {
   SharedPreferences.Editor editor = prefs.edit();
    //YOUR CODE TO SHOW DIALOG
    editor.apply();
}

完整代码

public class MainActivity extends Activity {

Button btnRegId;
EditText etRegId;
String regID;

GoogleCloudMessaging gcm;
String regid, url;

//String PROJECT_NUMBER = "90787073097";
String PROJECT_NUMBER = "440085976573";


String android_id, version, ver;


ImageView mega4, todayTips, latstnews, sportquiz, tipister;

TextView txtname;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // etRegId = (EditText) findViewById(R.id.edtvID);


    //********************For Rating APP **********************
    SharedPreferences sharedPrefs = MainActivity.this.getSharedPreferences("RATER", 0);
    if (sharedPrefs.getBoolean("NO THANKS", false)) {
        return;
    } else {
        SharedPreferences.Editor prefsEditor = sharedPrefs.edit();
        //YOUR CODE TO SHOW DIALOG
        long time = sharedPrefs.getLong("displayedTime", 0);
        if (time < System.currentTimeMillis() - 259200000) {
            displayDialog();
            prefsEditor.putLong("displayedTime", System.currentTimeMillis()).commit();
        }
        prefsEditor.apply();
    }


}

//dialog box Function for rating app.

private void displayDialog() {
    // TODO Auto-generated method stub
    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    //Yes button clicked
                    Intent in = new Intent(android.content.Intent.ACTION_VIEW);
                    in.setData(Uri.parse(url));
                    startActivity(in);
                    break;

                case DialogInterface.BUTTON_NEGATIVE:
                    //No button clicked
                    //Saving a boolean on no thanks button click

                    SharedPreferences prefs = MainActivity.this.getSharedPreferences("RATER", 0);
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean("NO THANKS", true);
                    editor.apply();
                    break;
            }
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Rate This App");
    builder.setMessage("You really seem to like this app, "
            + "since you have already used it %totalLaunchCount% times! "
            + "It would be great if you took a moment to rate it.")
            .setPositiveButton("Rate Now", dialogClickListener)
            .setNegativeButton("Latter", dialogClickListener)
            .setNeutralButton("No,thanks", dialogClickListener).show();

}
//End dialog box Function for rating app.
    }