How to cancel a n-second Thread.sleep in order to restart it in android?

64 views Asked by At

I'm trying to make a game, which includes a thread with a 10 seconds Thread.sleep in it, that gives the player a maximum time to react (when the sleep ends and he/she didn't choose anything, new stuff appears and it's a lost life). However if the player hit an option (no matter if it is the correct one or not), new stuff should appear immediately and the sleep should be "cancelled" in order to restart those 10 seconds. In other words, right now if at first I have 10 seconds, but after three seconds I hit an option, then I just have 7 seconds more before my interface change, when it should be another 10... I know I cannnot break that sleep, but how can I accomplish this? I'm not posting any line of code, because I would like to undertand the concept first, and then I can figure it out.

1

There are 1 answers

0
Mohammed Saleem On

To restard the app in android use the following Code:

PendingIntent intent = PendingIntent                                                .getApplicationContext(this, 0, new Intent(getApplicationContext .getIntent()),getActivity().getIntent().getFlags());                                                   

AlarmManager manager = (AlarmManager) getApplicationContext                                 .getSystemService(Context.ALARM_SERVICE);
                                 manager.set(AlarmManager.RTC,System.currentTimeMillis() + 3000,intent);

System.exit(2);