I'd like to make a comment system that only allows users to send about 20 comments per 5 minutes. This way I can be sure no one will send much spam...
I think I can count the comments using sharedpreferences. Each comments I read it and increase the number. But how about the last 5 minutes?
int value = sharedPref.getInt("count", 0);
value = value + 1;
editor.putInt("count", value);
Need to do a date/time difference operations on Now and Now - 5 minutes.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd:HH:mm");
String cTime = sdf.format(new Date());
Date date = formatter.parse(cTime);
Calendar futureTime = Calendar.getInstance();
futureTime.setTime(date);
futureTime.add(Calendar.MINUTES, 5); //This will be now + 5 minutes.
You can then reset your SharedPreferences variable.
Save the user, timestamp, and count in a key pair in your SharedPreference variable/data. You may have to create a custom object with two or more of those values as it's own custom class. Maybe User as the string, and the other two in the value, you can parse it out later. Use HashMap:
Map<String, String> aMap = new HashMap<String, String>();
People do this all the time in the game/app world. You get 100 coins every hour, etc, etc. I had implemented something in c#/Unity3D for iOS/Android. You get 100 coins every hour for free, you can do In App Purchases for more if you want, or make time pass quicker!