简体   繁体   English

如何在一天中每隔5次发布一次显示对话框?

[英]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. 这是我的代码它正在工作请告诉我我应该写的每个第5个启动对话框应该显示什么代码,意思是当用户启动应用程序在一天中第5次然后应该显示评级应用程序的对话框,同样每5个启动对话框应该显示。

 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 这是我的代码实际上我想在应用程序中实现应用程序评级对话框,应该在每天第5次启动时显示

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 此方法用于获取Date的getUniqueInt

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 此方法用于在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 此方法用于在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... 这样的事情,没试过,但你可以得到一个想法......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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