【问题标题】:How to implement a daily streak counter in android java?如何在android java中实现每日连胜计数器?
【发布时间】:2022-01-24 21:46:41
【问题描述】:

我想为用户实现每日连胜计数器。 如果用户在第二天返回应用程序,则连续计数应该增加 1,如果用户连续第二天没有返回应用程序,则连续计数应该再次返回到 1。

到目前为止,我已经尝试使用图像中的给定方法来实现这一点,但我的每日连胜记录始终显示为 1。 我认为这是因为它永远不会进入“其他”部分。

你们能帮我解决这个问题吗?任何类型的帮助都非常感谢。

【问题讨论】:

  • 请分享您的代码而不是截图,这样更容易在代码中查找错误,并且在回答时也很容易复制

标签: java android sharedpreferences counter android-savedstate


【解决方案1】:

我得到了我想要的东西:

if(getSharedPreferences(SHARED_PREF_TIME, MODE_PRIVATE).getInt(STREAKS,0)==0){

} 其他{

}

【讨论】:

    【解决方案2】:

    如果用户是第一次登录

    • 初始化上次登录日期并重置连续记录

    其他

    • 如果用户在同一天登录 什么都不做
    • 如果用户连续几天登录 增加连胜并更新上次登录日期
    • 如果上次登录日期超过一天 重置连续登录和上次登录日期

    下面是一个伪实现

    class ConsecutiveDayChecker {
    
        /**
         * Call this method when user login
         */
        public void onUserLogin() {
            DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
            Date date = new Date();
            String today = dateFormat.format(date);
            String lastLoginDay = getLastLoginDate();
    
            String yesterday = getYesterdayDate(dateFormat, date);
    
            if (lastLoginDay == null) {
                // user logged in for the first time
                updateLastLoginDate(today);
                incrementDays();
            } else {
                if (lastLoginDay.equals(today)) {
                    // User logged in the same day , do nothing
                } else if (lastLoginDay.equals(yesterday)) {
                    // User logged in consecutive days , add 1
                    updateLastLoginDate(today);
                    incrementDays();
                } else {
                    // It's been more than a day user logged in, reset the counter to 1
                    updateLastLoginDate(today);
                    resetDays();
                }
            }
        }
    
        private String getYesterdayDate(DateFormat simpleDateFormat, Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, -1);
            return simpleDateFormat.format(calendar.getTime());
        }
    
        /**
         * Update last login date into the storage
         * @param date
         */
        private void updateLastLoginDate(String date) {
            // SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
            // SharedPreferences.Editor editor = sharedPref.edit();
            // editor.putString("last_login_day", date);
            // editor.apply();
        }
    
        /**
         * Get last login date 
         * @return
         */
        private String getLastLoginDate() {
            // String lastLogin = null;
            // SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
            // lastLogin = sharedPref.getString("last_login_day", null);
            // return lastLogin;
        }
    
        private int getConsecutiveDays() {
            // int days = 0;
            // SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
            // days = sharedPref.getInt("num_consecutive_days", 0);
            // return days;
        }
    
        private void incrementDays() {
            // int days = getConsecutiveDays() + 1;
            // SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
            // SharedPreferences.Editor editor = sharedPref.edit();
            // editor.putInt("num_consecutive_days", days);
            // editor.apply();
        }
    
        private void resetDays() {
            // int days = 1;
            // SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
            // SharedPreferences.Editor editor = sharedPref.edit();
            // editor.putInt("num_consecutive_days", days);
            // editor.apply();
        }
    
        public int getStreak() {
            return getConsecutiveDays();
        }
    }
    

    【讨论】:

      【解决方案3】:

      savedInstanceState 将在应用启动时返回 null,您必须使用以下方式检查首次用户。

           if preferences.getInt(STREAKS) == null{ 
          
          }else {}
      

      【讨论】:

      • 感谢您在阅读了您的回答后得到了真正的答案
      猜你喜欢
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 2020-01-24
      • 2022-08-19
      相关资源
      最近更新 更多