user6405383 user6405383 - 1 year ago 87
Java Question

How to show dialog box once in a three days in application

here is my code it is working pls tell me code button for "no thanks" if user tap on this button then dialog box never show at all

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;

protected void onCreate(Bundle savedInstanceState) {

// etRegId = (EditText) findViewById(;

//********************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) {
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() {
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);

case DialogInterface.BUTTON_NEGATIVE:
//No button clicked

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.

Here is my code actually i want to implement app rating dialog box in application that should display once in three day

Answer Source

You have to initialize your SharedPreferences and Editor Object like this:

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


Just save a boolean when user cliks on no thanks and check it before showing the dialog. If it true then it will not show the dialog box.

//Saving a boolean on no thanks button click

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

Access it in your dialog showing method.

    SharedPreferences prefs = mContext.getSharedPreferences("RATER", 0);
    if (prefs.getBoolean("NO THANKS", false)) {
    }else {
   SharedPreferences.Editor editor = prefs.edit();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download