1

I have created an imagebutton, which should allow only 10 clicks daily. Now I want to use thread to sleep the imagebutton for 24 hours, after that which should allow again for 10 clicks next day(after 24 hours). How should I use thread to sleep the button click for 24 hours. Please suggest me how do I solve this.

ActivityTwo.java:

public class ActivityTwo extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_two);
    ImageButton playbtn1 = (ImageButton) findViewById(R.id.playButton1);
    playbtn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), ActivityTwo.class);
            startActivity(intent);
            intent.putExtra("scoretwo",1);
            setResult(RESULT_OK,intent);
        }
    });

MainActivity.java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int RC_ACT_TWO = 1;
    TextView scoreCount = (TextView) findViewById(R.id.score_points);
    String score = scoreCount.getText().toString();
    int scorevalue = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageButton viewbtn1 = (ImageButton) findViewById(R.id.view1);
        viewbtn1.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {
                Intent intent1 = new
                Intent(getApplicationContext(), ActivityTwo.class);
                startActivity(intent1);
                startActivityForResult(intent1, RC_ACT_TWO);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_ACT_TWO) {
            if (resultCode == RESULT_OK) {
                int incre = 0;
                int add = data.getIntExtra("scoretwo", 0);
                scorevalue = Integer.parseInt(score);
                if (add != 0) {
                    incre = incre + add;
                    if (incre > 0 && incre < 11) {
                        scorevalue = scorevalue + incre;
                        scoreCount.setText(scorevalue);
                    }
                    else {}
                }
                else {
                    scoreCount.setText("-1");
                }
            }
        }
    }
Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95

3 Answers3

0

Here is one possibility: You could use Android's SharedPreferences for that. Save the timestamp of the first click, as well as the amount of clicks so far..

When clicking on the button, or when starting the activity, check the saved amount of clicks. If it reached 10, you disable the button.

At the start of your activity, also check the saved timestamp. If it is older than 24 hours, reenable the button again and reset the saved amount of clicks in the prefs.

For more information, have a look at this link.

QBrute
  • 4,405
  • 6
  • 34
  • 40
0

Thread.Sleep is not a good way for doing it since it put the main thread sleeping for the time you set (so don't use it on the main thread), and it will suppose that the user has for 24h the app open.

So.

  1. If you sleep for 24h, you are not setting the limit daily but you are setting it every 24h from the last time it was done. IE: if I click 10 times at 23.59 of monday, I will be able to do it again at 23.59 of tuesday, so I lost one day.
  2. As mentioned above, thread.sleep is putting the main thread sleeping, so if I close the app and then I open it back, you lost the timer. + it's not a good practice.

Said that, one way could be using SharedPreferences (This is ONE WAY, there are many others which are also probably better than this, but this was the first which came in my mind).

Now, using SharedPreferences (Useful link), store the number of clicks done and the day the user clicked.

At this point, every app's launch, read the day and the count.

If the day is the same as today, read the count and everytime the user clicks, add 1. At 10, disable the button with a message (simply add if clicks >= 10 do disable).

Every time the user clicks the button, add 1 to the counter and save it in sharedpreferences, saving also the current day.

This way you are done, because if the user clicked 10 times today, you will have this value stored in the SharedPreferences. If the day has changed, you will notice that the day saved in SharedPreferences is different from the current day.

As I said above this is one way, you can find many others. I put just the logics here with useful links so you are surely able to reproduce the code yourself, for any qyestion, doubt I'm here.

Community
  • 1
  • 1
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
  • Thank you for your suggestion. I liked the way you described all above. Can you please help me how should i use the sharedpreference in my project and where should i use it. Give me some examples how to use sharedpreference. – kalishankar karmokar Jan 25 '17 at 09:48
  • @kalishankarkarmokar I added a link (the "Useful link"one) which is a reference to a SO question which has a really deep and well posted explaination about SharedPreferences, mine would just be a copy-paste :) – Pier Giorgio Misley Jan 25 '17 at 09:55
0

Putting your thread to sleep is not a good solution. The best way is to store the number of times the image was clicked, and for that you have many solutions, from the sharedPreferences to a sql database (but if the only data you store is this number the database would be overkill).

Waylan
  • 91
  • 1
  • 2
  • 9