av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

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

最近有個(gè)需求,要求啟動(dòng)頁(yè)的攔截頁(yè)每天只能顯示3次,超過(guò)三次就顯示別的頁(yè)面,然后到第二天才可以再次顯示,利用SharePreferences保存天數(shù)和每天的次數(shù),大概是思路是:判斷 如果是同一天,就去拿保存的次數(shù),當(dāng)次數(shù)小于3才執(zhí)彈出攔截頁(yè),然后,每次彈出,次數(shù)就加1,并且保存次數(shù)和當(dāng)天的時(shí)間;如果不是同一天,就把次數(shù)賦值為1,并且把當(dāng)天賦值給最后訪問(wèn)的時(shí)間,然后保存當(dāng)前的次數(shù)。具體實(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í)行攔截頁(yè)操作; //修改SharePreferences日期為當(dāng)前日期,并記錄次數(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í)行攔截頁(yè)操作; 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-----: '+'超過(guò)三次'); } } } }); }}

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

總結(jié)

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

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 亚洲免费在线观看 | 国产中文在线 | 久操国产 | 黄色一及片 | 91久久精品视频 | 中文字幕偷拍 | 中文字幕黄色片 | 中文字幕免费观看 | 久久福利视频导航 | 中文字幕av久久爽一区 | 亚洲激情五月 | 国产一区在线观看视频 | 国内精品一区二区三区 | 欧美日韩精品一区二区 | 成年人视频网站 | 欧美三级韩国三级日本三斤在线观看 | 亚洲永久免费 | www.com国产 | 色婷婷在线视频 | 性色av网站| 中文字幕在线免费观看视频 | 久久精品国产一区二区 | 日本免费毛片 | 婷婷av在线 | 日韩免费一区二区 | 亚洲伊人影院 | 亚洲欧美日韩国产精品 | 亚洲一区二区在线免费观看 | 欧美一级片在线 | 国产黄a三级三级三级看三级男男 | 国产乱淫av片免费 | 国产精品一区一区三区 | 五月婷婷六月激情 | 欧美一级片网站 | 国产在线第一页 | 亚洲福利网站 | 国产成人精品一区二区三区视频 | 天天色天天爱 | 超碰福利在线 | 欧美精品久久久久 | 四虎网站在线观看 |