前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android 限制某个操作每天只能操作指定的次数(示例代码详解)

android 限制某个操作每天只能操作指定的次数(示例代码详解)

作者头像
砸漏
发布2020-10-16 10:06:48
6490
发布2020-10-16 10:06:48
举报
文章被收录于专栏:恩蓝脚本

最近有个需求,要求启动页的拦截页每天只能显示3次,超过三次就显示别的页面,然后到第二天才可以再次显示,利用SharePreferences保存天数和每天的次数,大概是思路是:判断 如果是同一天,就去拿保存的次数,当次数小于3才执弹出拦截页,然后,每次弹出,次数就加1,并且保存次数和当天的时间;如果不是同一天,就把次数赋值为1,并且把当天赋值给最后访问的时间,然后保存当前的次数。具体实现如下:

代码语言:javascript
复制
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. */

代码语言:javascript
复制
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 限制操作次数内容请搜索ZaLou.Cn以前的文章或继续浏览下面的相关文章希望大家以后多多支持ZaLou.Cn!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档