android 限制某个操作每天只能操作指定的次数(示例代码详解)
程序员文章站
2023-11-06 17:53:40
最近有个需求,要求启动页的拦截页每天只能显示3次,超过三次就显示别的页面,然后到第二天才可以再次显示,利用sharepreferences保存天数和每天的次数,大概是思路是:判断 如果是同一天,就去拿...
最近有个需求,要求启动页的拦截页每天只能显示3次,超过三次就显示别的页面,然后到第二天才可以再次显示,利用sharepreferences保存天数和每天的次数,大概是思路是:判断 如果是同一天,就去拿保存的次数,当次数小于3才执弹出拦截页,然后,每次弹出,次数就加1,并且保存次数和当天的时间;如果不是同一天,就把次数赋值为1,并且把当天赋值给最后访问的时间,然后保存当前的次数。具体实现如下:
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执行拦截页操作; //修改sharepreferences日期为当前日期,并记录次数一次; frequency = 1; log.i(tag, "onclick-----: "+"不是同一天执行次数"+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执行拦截页操作; log.i(tag, "onclick-----: "+"同一天执行次数"+frequency); frequency++; sharepreferencesutils.putintvalue(twoactivity.this,"time_and_frequency","lastday",lastday); sharepreferencesutils.putintvalue(twoactivity.this,"time_and_frequency","frequency",frequency); }else { //todo执行别的操作 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 限制某个操作每天只能操作指定的次数(示例代码详解)的文章就介绍到这了,更多相关android 限制操作次数内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 至少我还活着,对吧