android 限制某个操作每天只能操作指定的次数(示例代码详解)
最近有个需求,要求启动页的拦截页每天只能显示3次,超过三次就显示别的页面,然后到第二天才可以再次显示,利用SharePreferences保存天数和每天的次数,大概是思路是:判断如果是同一天,就去拿保存的次数,当次数小于3才执弹出拦截页,然后,每次弹出,次数就加1,并且保存次数和当天的时间;如果不是同一天,就把次数赋值为1,并且把当天赋值给最后访问的时间,然后保存当前的次数。具体实现如下:
packagecom.example.demo1.test;
importandroid.support.v7.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.View;
importcom.example.demo1.R;
importjava.util.Calendar;
publicclassTwoActivityextendsAppCompatActivity{
privatestaticfinalStringTAG="TwoActivity";
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
findViewById(R.id.test).setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewview){
intfrequency=SharePreferencesUtils.getIntValue(TwoActivity.this,"time_and_frequency","frequency");
inttoday=Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
intlastDay=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);
}elseif(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-WaitFunInc.AllRightsReserved.
*/
packagecom.example.demo1.test;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.content.SharedPreferences;
importandroid.content.SharedPreferences.Editor;
importjava.util.Map;
publicclassSharePreferencesUtils{
privatefinalstaticStringTAG=SharePreferencesUtils.class.getName();
privatefinalstaticSharedPreferencesgetSharePreferences(Contextcontext,StringfileName){
returncontext.getSharedPreferences(fileName,Activity.MODE_PRIVATE);
}
publicstaticStringgetStrValue(Contextcontext,StringfileName,Stringkey){
returngetSharePreferences(context,fileName).getString(key,"");
}
publicstaticintgetIntValue(Contextcontext,StringfileName,Stringkey){
returngetSharePreferences(context,fileName).getInt(key,0);
}
publicstaticbooleangetBooleanValue(Contextcontext,StringfileName,Stringkey){
returngetSharePreferences(context,fileName).getBoolean(key,false);
}
publicstaticvoidputBooleanValue(Contextcontext,StringfileName,Stringkey,booleanvalue){
Editoreditor=getSharePreferences(context,fileName).edit();
editor.putBoolean(key,value);
editor.commit();
editor.clear();
editor=null;
}
publicstaticvoidputStringValue(Contextcontext,StringfileName,Stringkey,Stringvalue){
Editoreditor=getSharePreferences(context,fileName).edit();
editor.putString(key,value);
editor.commit();
editor.clear();
editor=null;
}
publicstaticvoidputIntValue(Contextcontext,StringfileName,Stringkey,intvalue){
Editoreditor=getSharePreferences(context,fileName).edit();
editor.putInt(key,value);
editor.commit();
editor.clear();
editor=null;
}
publicstaticvoidputMapStringValue(Contextcontext,StringfileName,MapeditorValue){
Editoreditor=getSharePreferences(context,fileName).edit();
for(Map.Entryentry:editorValue.entrySet()){
Stringkey=entry.getKey();
Stringvalue=entry.getValue();
editor.putString(key,value);
}
editor.commit();
editorValue.clear();
editorValue=null;
}
publicstaticvoidputMapIntegerValue(Contextcontext,StringfileName,MapeditorValue){
Editoreditor=getSharePreferences(context,fileName).edit();
for(Map.Entryentry:editorValue.entrySet()){
Stringkey=entry.getKey();
Integervalue=entry.getValue();
editor.putInt(key,value);
}
editor.commit();
editorValue.clear();
editorValue=null;
}
}
总结
到此这篇关于android限制某个操作每天只能操作指定的次数(示例代码详解)的文章就介绍到这了,更多相关android限制操作次数内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!