1. 程式人生 > 程式設計 >android 限制某個操作每天只能操作指定的次數(示例程式碼詳解)

android 限制某個操作每天只能操作指定的次數(示例程式碼詳解)

最近有個需求,要求啟動頁的攔截頁每天只能顯示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,"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,"lastDay",today);
   SharePreferencesUtils.putIntValue(TwoActivity.this,"frequency",frequency);
  }else if(today == lastDay){
   if(frequency < 3) {
   //TODO執行攔截頁操作;
   Log.i(TAG,"onClick-----: "+"同一天執行次數"+frequency);
   frequency++;
   SharePreferencesUtils.putIntValue(TwoActivity.this,lastDay);
    SharePreferencesUtils.putIntValue(TwoActivity.this,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,fileName).getInt(key,0);
 }
 public static boolean getBooleanValue(Context context,fileName).getBoolean(key,false);
 }
 public static void putBooleanValue(Context context,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 value) {
 Editor editor = getSharePreferences(context,fileName).edit();
 editor.putString(key,value);
 editor.commit();
 editor.clear();
 editor = null;
 }
 public static void putIntValue(Context context,int value) {
 Editor editor = getSharePreferences(context,fileName).edit();
 editor.putInt(key,value);
 editor.commit();
 editor.clear();
 editor = null;
 }
 public static void putMapStringValue(Context context,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,Integer> editorValue) {
 Editor editor = getSharePreferences(context,Integer> entry : editorValue.entrySet()) {
  String key = entry.getKey();
  Integer value = entry.getValue();
  editor.putInt(key,value);
 }
 editor.commit();
 editorValue.clear();
 editorValue = null;
 }
}

總結

到此這篇關於android 限制某個操作每天只能操作指定的次數(示例程式碼詳解)的文章就介紹到這了,更多相關android 限制操作次數內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!