android 限制某個(gè)操作每天只能操作指定的次數(shù)(示例代碼詳解)
最近有個(gè)需求,要求啟動(dòng)頁的攔截頁每天只能顯示3次,超過三次就顯示別的頁面,然后到第二天才可以再次顯示,利用SharePreferences保存天數(shù)和每天的次數(shù),大概是思路是:判斷 如果是同一天,就去拿保存的次數(shù),當(dāng)次數(shù)小于3才執(zhí)彈出攔截頁,然后,每次彈出,次數(shù)就加1,并且保存次數(shù)和當(dāng)天的時(shí)間;如果不是同一天,就把次數(shù)賦值為1,并且把當(dāng)天賦值給最后訪問的時(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í)行攔截頁操作;
//修改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í)行攔截頁操作;
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;
}
}
總結(jié)
到此這篇關(guān)于android 限制某個(gè)操作每天只能操作指定的次數(shù)(示例代碼詳解)的文章就介紹到這了,更多相關(guān)android 限制操作次數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android中的廣播(BroadCast)詳細(xì)介紹
這篇文章主要介紹了Android中的廣播(BroadCast)詳細(xì)介紹,本文講解了什么是廣播、廣播有什么用、實(shí)現(xiàn)廣播、動(dòng)態(tài)注冊方式、配置文件方式等內(nèi)容,需要的朋友可以參考下2015-03-03
Android實(shí)現(xiàn)創(chuàng)意LoadingView動(dòng)畫效果
這篇文章主要介紹了Android實(shí)現(xiàn)創(chuàng)意LoadingView動(dòng)畫效果的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android多功能時(shí)鐘開發(fā)案例(實(shí)戰(zhàn)篇)
這篇文章主要為大家詳細(xì)介紹了Android多功能時(shí)鐘開發(fā)案例,開發(fā)了時(shí)鐘、鬧鐘、計(jì)時(shí)器和秒表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
Android studio gradle環(huán)境變量配置教程
這篇文章主要為大家詳細(xì)介紹了Android studio gradle環(huán)境變量配置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Kotlin?Dispatchers協(xié)程調(diào)度器源碼深入分析
Kotlin協(xié)程不是什么空中閣樓,Kotlin源代碼會(huì)被編譯成class字節(jié)碼文件,最終會(huì)運(yùn)行到虛擬機(jī)中。所以從本質(zhì)上講,Kotlin和Java是類似的,都是可以編譯產(chǎn)生class的語言,但最終還是會(huì)受到虛擬機(jī)的限制,它們的代碼最終會(huì)在虛擬機(jī)上的某個(gè)線程上被執(zhí)行2022-11-11
MVVM和MVVMLight框架介紹及在項(xiàng)目中的使用詳解
這篇文章主要為大家介紹了MVVM和MVVMLight的介紹及在項(xiàng)目中的使用詳解有需要的朋友可以借鑒參考下,祝大家除夕快樂多多進(jìn)步2022-01-01
Gradle?Build?Cache引發(fā)的Task緩存編譯問題
這篇文章主要為大家介紹了Gradle?Build?Cache引發(fā)的Task緩存編譯問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

