简体   繁体   中英

How to display dialog box every 5th launch in a day?

here is my code it is working pls tell me what code i should write to every 5th launch the dialog box should show, mean when user launch application 5th time in a day then should show dialog box for rating app,similarly every 5th launch dialog box should be show.

 public class MainActivity extends Activity {


    String android_id,version,ver;

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


    //********************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.
  }

Here is my code actually i want to implement app rating dialog box in application that should display every 5th time launch in a day

Use below method to check Launch Count.

private void checkLaunchCount()
{
    int sessionUniqueId = mSharedPref.getInt("DATE_UNIQUE", 0);
    int launchCount = mSharedPref.getInt("LAUNCH_COUNT", 0);
    if(sessionUniqueId != 0)
    {
        int todayUniqueId = getUniqueNumberFromDate();
        if(todayUniqueId == sessionUniqueId)
        {
            if(launchCount >= 5)
            {
                //Show Dialog
            }
            else
            {
                updateLaunchCount(launchCount);
            }
        }
        else
        {
            updateUniqueDateId();
            updateLaunchCount(launchCount);
        }

    }
    else
    {
        updateUniqueDateId()
        updateLaunchCount(launchCount);
    }
}

This method used to getUniqueInt for the Date

private int getUniqueNumberFromDate()
{
    Date date = new Date();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.DAY_OF_YEAR);
}

This method use to save day in SharedPref

private static SharedPreferences mSharedPref;
private void updateUniqueDateId()
{
    if(mSharedPref == null)
        mSharedPref = getActivity().getSharedPreferences(getActivity().getPackageName(), Activity.MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = mSharedPref.edit();
    prefsEditor.putInt("DATE_UNIQUE", getUniqueNumberFromDate()).commit();
}

This method use to save count in SharedPref

 private void updateLaunchCount(int launchCount)
{
    launchCount++;
    SharedPreferences.Editor prefsEditor = mSharedPref.edit();
    prefsEditor.putInt("LAUNCH_COUNT", launchCount).commit();
}

You can get a Calendar values and one counter, and store its value to the preferences.

Calendar rightNow = Calendar.getInstance();
int year = rightNow.get(rightNow.YEAR);
int dayOfYear = rightNow.get(rightNow.DAY_OF_YEAR);

int count = mPreferences.getInt("launchCount",0);
int storedYear = mPreferences.getInt("storedYear",0);
int storedDayOfYear = mPreferences.getInt("storedDayOfYear",0);
if (storedYear == year && storedDayOfYear == dayOfYear) {
    if (count == 5) {
       //rate
    }
    count++;
    mPreferences.edit().putInt("launchCount",count).commit();
} else {
    mPreferences.edit().putInt("year",count);
    mPreferences.edit().putInt("dayOfYear",count);
    count = 1;
    mPreferences.edit().putInt("launchCount",count);
    mPreferences.edit().commit();
}

Something like that, have not tried it, but you can get an idea...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM