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

时间:2016-10-30 15:56:00

标签: java android sharedpreferences alertdialog

这是我正在运行的代码请告诉我应该在每个第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.
  }

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

2 个答案:

答案 0 :(得分:1)

使用以下方法检查启动计数。

[
  {"lat": 28.418871185756046, "lng": 77.03988238780516, "name":"00", "location": "", "sync_time":"2016-10-12 15:58:05", "vehicleNo": "Abhishek"},
  {"lat": 28.414201027385680, "lng": 77.04124510288238, "name":"01", "location": "", "sync_time":"2016-10-12 15:59:05", "vehicleNo": "Abhishek"},
  {"lat": 28.410076539302824, "lng": 77.04258605449217, "name":"02", "location": "", "sync_time":"2016-10-12 16:00:05", "vehicleNo": "Abhishek"},
  {"lat": 28.407142409664587, "lng": 77.04351425170898, "name":"03", "location": "", "sync_time":"2016-10-12 16:02:05", "vehicleNo": "Abhishek"},
  {"lat": 28.404980594548426, "lng": 77.04391643016356, "name":"04", "location": "", "sync_time":"2016-10-12 16:04:05", "vehicleNo": "Abhishek"}
]

此方法用于getUniqueInt用于日期

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);
    }
}

此方法用于在SharedPref中保存日期

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

此方法用于在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();
}

答案 1 :(得分:0)

您可以获取日历值和一个计数器,并将其值存储到首选项中。

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();
}

这样的事情,没试过,但你可以得到一个想法......