亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

android 限制某個操作每天只能操作指定的次數(shù)(示例代碼詳解)

瀏覽:87日期:2022-09-23 17:07:51

最近有個需求,要求啟動頁的攔截頁每天只能顯示3次,超過三次就顯示別的頁面,然后到第二天才可以再次顯示,利用SharePreferences保存天數(shù)和每天的次數(shù),大概是思路是:判斷 如果是同一天,就去拿保存的次數(shù),當次數(shù)小于3才執(zhí)彈出攔截頁,然后,每次彈出,次數(shù)就加1,并且保存次數(shù)和當天的時間;如果不是同一天,就把次數(shù)賦值為1,并且把當天賦值給最后訪問的時間,然后保存當前的次數(shù)。具體實現(xiàn)如下:

package com.example.demo1.test; import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View; import com.example.demo1.R; import java.util.Calendar; public class TwoActivity extends AppCompatActivity { private static final String TAG = 'TwoActivity'; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); findViewById(R.id.test).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int frequency = SharePreferencesUtils.getIntValue(TwoActivity.this,'time_and_frequency','frequency'); int today = Calendar.getInstance().get(Calendar.DAY_OF_YEAR); int lastDay = SharePreferencesUtils.getIntValue(TwoActivity.this,'time_and_frequency','lastDay'); Log.i(TAG, 'onClick-----: '+'today:'+today); Log.i(TAG, 'onClick-----: '+'lastDay:'+lastDay); Log.i(TAG, 'onClick-----: '+'frequency:'+frequency); if(today != lastDay) { //TODO執(zhí)行攔截頁操作; //修改SharePreferences日期為當前日期,并記錄次數(shù)一次; frequency = 1; Log.i(TAG, 'onClick-----: '+'不是同一天執(zhí)行次數(shù)'+frequency); //把today賦值給lastDay 讓today == lastDay SharePreferencesUtils.putIntValue(TwoActivity.this,'time_and_frequency','lastDay',today); SharePreferencesUtils.putIntValue(TwoActivity.this,'time_and_frequency','frequency',frequency); }else if(today == lastDay){ if(frequency < 3) { //TODO執(zhí)行攔截頁操作; Log.i(TAG, 'onClick-----: '+'同一天執(zhí)行次數(shù)'+frequency); frequency++; SharePreferencesUtils.putIntValue(TwoActivity.this,'time_and_frequency','lastDay',lastDay); SharePreferencesUtils.putIntValue(TwoActivity.this,'time_and_frequency','frequency',frequency); }else { //TODO執(zhí)行別的操作 Log.i(TAG, 'onClick-----: '+'超過三次'); } } } }); }}

SharePreferencesUtils代碼如下:

/* * Copyright (c) 2017- WaitFun Inc. All Rights Reserved. */

package com.example.demo1.test;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import java.util.Map;public class SharePreferencesUtils { private final static String TAG = SharePreferencesUtils.class.getName(); private final static SharedPreferences getSharePreferences(Context context, String fileName) { return context.getSharedPreferences(fileName, Activity.MODE_PRIVATE); } public static String getStrValue(Context context, String fileName, String key) { return getSharePreferences(context, fileName).getString(key, ''); } public static int getIntValue(Context context, String fileName, String key) { return getSharePreferences(context, fileName).getInt(key, 0); } public static boolean getBooleanValue(Context context, String fileName, String key) { return getSharePreferences(context, fileName).getBoolean(key, false); } public static void putBooleanValue(Context context, String fileName, String key, boolean value) { Editor editor = getSharePreferences(context, fileName).edit(); editor.putBoolean(key, value); editor.commit(); editor.clear(); editor = null; } public static void putStringValue(Context context, String fileName, String key, String value) { Editor editor = getSharePreferences(context, fileName).edit(); editor.putString(key, value); editor.commit(); editor.clear(); editor = null; } public static void putIntValue(Context context, String fileName, String key, int value) { Editor editor = getSharePreferences(context, fileName).edit(); editor.putInt(key, value); editor.commit(); editor.clear(); editor = null; } public static void putMapStringValue(Context context, String fileName, Map<String, String> editorValue) { Editor editor = getSharePreferences(context, fileName).edit(); for (Map.Entry<String, String> entry : editorValue.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); editor.putString(key, value); } editor.commit(); editorValue.clear(); editorValue = null; } public static void putMapIntegerValue(Context context, String fileName, Map<String, Integer> editorValue) { Editor editor = getSharePreferences(context, fileName).edit(); for (Map.Entry<String, Integer> entry : editorValue.entrySet()) { String key = entry.getKey(); Integer value = entry.getValue(); editor.putInt(key, value); } editor.commit(); editorValue.clear(); editorValue = null; }}

總結

到此這篇關于android 限制某個操作每天只能操作指定的次數(shù)(示例代碼詳解)的文章就介紹到這了,更多相關android 限制操作次數(shù)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Android
相關文章:
主站蜘蛛池模板: 国产黄a三级三级三级 | 国产成人精品1沈娜娜 | 国产丝袜福利 | 九九热视频这里只有精品 | 国产精品青草久久久久福利99 | 麻豆视频秘密入口 | 免费 欧美 自拍 在线观看 | 久久精品综合视频 | 国产青青操 | 日韩毛片网 | 国产精品久久久久国产精品 | 在线日本看片免费人成视久网 | 美女隐私视频黄www免费 | 久久只有这才是精品99 | 中日韩欧美一级毛片 | 成人网视频在线观看免费 | 涩色婷婷狠狠第四四房社区奇米 | 欧美一区不卡二区不卡三区 | 午夜视频在线观看一区 | 国产亚洲精品资源一区 | 精品国产一级在线观看 | 1级黄色| 免费的三级毛片 | www.99精品 | 看片地址| 色伊人色成人婷婷六月丁香 | 久久久精品日本一区二区三区 | 狠狠狠色丁香婷婷综合久久五月 | 日韩中文字幕在线播放 | 美国黑人特大一级毛片 | 精品在线观看免费 | 国产区第一页 | 久久精品视频2 | 国产在线麻豆精品 | 日本96在线精品视频免费观看 | 亚洲精品二区中文字幕 | 一区二区三区四区日韩 | 日韩视频一区二区 | 91青青草视频 | 路线1路线二线路三国产 | 中文字幕一区二区精品区 |