简体   繁体   中英

How to limit click event only 3 times per day, and after that disable it and enable click only on next day?

I am making Spinning wheel for prize, I am able to disable click event after 3 click but unable to enable click in another day, I just want to make user click only 3 times per day and disable click event for that day and only enable in next day

public class MainActivity extends AppCompatActivity implements SpinningWheelView.OnRotationListener<String> {

private SpinningWheelView wheelView;

private TextView rotate,spinCount,spinPoint;
int COUNT=0;
int points=0;

private SharedPreferences mPrefs;
private SharedPreferences.Editor editor;
private String sharedPref="MY_PREF";

int count= 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mPrefs=getSharedPreferences(sharedPref,Context.MODE_PRIVATE);
    mPrefs.getInt("count",0);
    editor=mPrefs.edit();


    wheelView = (SpinningWheelView) findViewById(R.id.wheel);

    rotate = (TextView) findViewById(R.id.rotate);
    spinCount=(TextView)findViewById(R.id.spin_count);
    spinPoint=(TextView)findViewById(R.id.spin_point);


    wheelView.setItems(R.array.dummy);
    wheelView.setOnRotationListener(this);

    rotate.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public void onClick(View v) {
            int i = mPrefs.getInt("count",0);

            if (i<3){
                count++;
                editor.putInt("count",count);
                editor.apply();
                wheelView.rotate(80, 3000, 50);
            }else {
                rotate.setEnabled(false);

              //how can it be enabled on another day
            }


        }
    });
}

I can achieve it with Shared Preferences, but Shared Preferences can be cleared by clearing data and cache, which is not good for my app as I want to store their points on the cloud.

Most Simple Solution
isTodayClickable =true/false

OnCreate() get the time stamp and set the clicable true/ clicked and clicked date Both save in shared pref after 3 click enable it to false save it in pref write logic if there is shared pref for date chek if the date is more than one day then set it to true and save it in pref

but unable to enable click in another day

You can store the date/time of the first click and reset it after 24 hours, or reset at midnight. this is not recommended as you know, because clients can bypass these kind of limitations easily.

back to the main question

How to limit click event only 3 time per day

1. make a UUID for that client installation: The Description in google's docs is far beyond good.

2. Add a field in your server's database/table for this UUID's clicks. The UUID is your primary key. increase clicks field after every click. For sending a request this simple to your server, you do not really need a library, but AsyncHttpClient makes it easy.

If the internet is not available, tell your users about it. Do not continue offline since they can easily cheat.

3. store the date/time of that first click in your server.

4. Make a simple stored procedure in your SQL database that resets the dates at 00:00 .

Create date object store in shared preferences with number of button click. on next day create new date and map with shared preferences stored date. if match then disable button else enable. here is the example below.

button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

    int clicks = 0;
    clicks++;

    if (clicks >3){
        button.setEnabled(false);
    }

    SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", this.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt("clicks", clicks);
    editor.putString("date",date.toString());
    editor.apply();
}
});

Happy coding!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM