android Limit an operation to a specified number of times per day of Explanation

  • 2021-12-04 11:23:12
  • OfStack

There is a demand recently, The blocking page that requires the startup page can only be displayed 3 times a day, More than 3 times will display other pages, and then it can be displayed again on the second day. Using SharePreferences to save days and times every day, the idea is: judge if it is the same day, take the number of times to save, and when the number is less than 3, hold the pop-up interception page, and then, every time it pops up, the number will be increased by 1, and the number of times and the time of the day will be saved; If it is not the same day, assign the number of times to 1, and give the current talent value to the last access time, and then save the current number of times. The specific implementation is as follows:


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 Perform page interception operation; 
 
   // Modify SharePreferences The date is the current date and the number of times is recorded 1 Times; 
   frequency = 1;
   Log.i(TAG, "onClick-----: "+" Not the same 1 Number of executions per day "+frequency);
   // Put today Assign a value to lastDay  Jean 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 Perform page interception operation; 
   Log.i(TAG, "onClick-----: "+" Same as 1 Number of executions per day "+frequency);
   frequency++;
   SharePreferencesUtils.putIntValue(TwoActivity.this,"time_and_frequency","lastDay",lastDay);
    SharePreferencesUtils.putIntValue(TwoActivity.this,"time_and_frequency","frequency",frequency);
   }else {
   //TODO Perform another operation 
   Log.i(TAG, "onClick-----: "+" Exceed 3 Times ");
   }
  }
  }
 });
 }
}

The SharePreferencesUtils code is as follows:

/*
* 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;
 }
}

Summarize


Related articles: