允许用户在Android中每天点击一次菜单



我正在创建一个程序,在actionbarsherlock中有一个菜单项。现在我希望用户每天点击该菜单一次,而不是更多。我怎样才能做到这一点呢?以下是目前为止的内容:

int hour = today.get(Calendar.HOUR_OF_DAY);
if (hour < 24) {
    try {
        if (gotGenders.contentEquals("Male")) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                    Monitor.this);
            alertDialog.setTitle(gotNames);
            alertDialog.setMessage("You are currently burning "
                    + caloriesForMen() + " calories per hour");
            alertDialog.setPositiveButton("Save",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub
                        }
                    });
            alertDialog.setNegativeButton("Discard",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub
                            dialog.dismiss();
                        }
                    });
            alertDialog.setCancelable(false);
            showAlertDialog = alertDialog.create();
            showAlertDialog.show();
        }
        else if (gotGenders.contentEquals("Female")) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                    Monitor.this);
            alertDialog.setTitle(gotNames);
            alertDialog.setMessage("You are currently burning "
                    + caloriesForWomen() + " calories per hour");
            alertDialog.setPositiveButton("Save",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub
                            // dialog.dismiss();
                        }
                    });
            alertDialog.setNegativeButton("Discard",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub
                            dialog.dismiss();
                        }
                    });
            alertDialog.setCancelable(false);
            showAlertDialog = alertDialog.create();
            showAlertDialog.show();
            count = 1;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        AlertDialog.Builder errorDialog = new AlertDialog.Builder(
                Monitor.this);
        errorDialog.setTitle("No User Selected!");
        // errorDialog.setMessage("You are currently burning " +
        // caloriesForWomen() + " per hour");
        errorDialog.setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,
                            int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });
        errorDialog.setCancelable(false);
        errorAlertDialog = errorDialog.create();
        errorAlertDialog.show();
    }
} else {
    count = 0;
    AlertDialog.Builder waitDialog = new AlertDialog.Builder(
            Monitor.this);
    waitDialog.setTitle("Wait after the day is over");
    waitDialog.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });
    waitDialog.setCancelable(false);
    waitAlertDialog = waitDialog.create();
    waitAlertDialog.show();
}

这允许我多次选择菜单,我如何限制它每天只点击一次?

要实现这一点,您应该将上次访问的日期存储在某处(db, sharedpreferences,在一个文件中),然后:

long lastVistedDateTime = getLastVistedDateTime();
int lastDay = new Date(lastVistedDateTime).getDay();
int today = Calendar.get(Calendar.DATE);
if (today != lastDay){
    //an other day
    setLastVistedDateTime(Calendar.getInstance().getTimeInMillis());
}

要与当前区域设置比较日期,使用:

String lastDate = DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(lastVistedDateTime));
String nowDate = DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date());
if (!nowDate.equals(lastDate)){
    //an other day
    setLastVistedDateTime(Calendar.getInstance().getTimeInMillis());
}

我认为总的来说gezdy的回答方法是正确的,但是我想提出一个简化的方法。

日历。DAY_OF_YEARhttp://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html DAY_OF_YEAR

给出一年中的当前日期(1-365)。因此,与其用毫秒做任何数学运算,不如抓取该值并存储它,并在随后的尝试中进行比较。应该将gezdy的答案中的代码减少到大约3行(同时做完全相同的事情)。

唯一可能的缺点是

  1. 用户可以改变他们设备上的时钟
  2. 用户可能会遇到一个意外的预防应用程序的使用,如果他们只使用一次应用程序然后在一年后的同一天回来。(不太可能)。

相关内容

  • 没有找到相关文章

最新更新