Android APP首次/首次今日启动判断

作者: i小灰 | 来源:发表于2020-09-13 11:08 被阅读0次

1.APP首次启动判断

2.APP今日启动判断

上菜:

    /**
     * 判断是否是首次启动
     * 
     *  此方法启动调用第一次是准确值,如果在一次启动中多次调用,即使是首次启动,第二次调用也会变成非首次启动,若需要多次获取,可以赋新值使用,每次启动只能调用此方法一次,赋值获取
     * @param context
     * @return
     */
    public static boolean isFirstStart(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(
                "NB_FIRST_START", 0);
        Boolean isFirst = preferences.getBoolean("FIRST_START", true);
        if (isFirst) {// 第一次
            preferences.edit().putBoolean("FIRST_START", false).commit();
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是否是今日首次启动APP
     * @param context
     * @return
     */
    public static boolean isTodayFirstStartApp(Context context) {
        try {
            SharedPreferences preferences = context.getSharedPreferences("NB_TODAY_FIRST_START_APP", context.MODE_PRIVATE);
            String svaeTime = preferences.getString("startAppTime", "2020-01-08");
            String todayTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

            if (!TextUtils.isEmpty(todayTime) && !TextUtils.isEmpty(svaeTime)) {
                if(!svaeTime.equals(todayTime)) {
                    preferences.edit().putString("startAppTime", todayTime).commit();
                    return true;
                }
            }

        }catch (Exception e){
            log(TAG, "是否为今日首次启动APP,获取异常:"+e.toString());
            return true;
        }
        return  false;

    }

相关文章

网友评论

    本文标题:Android APP首次/首次今日启动判断

    本文链接:https://www.haomeiwen.com/subject/ybpfektx.html