0

I am trying to build a knowledge based game , in which user plays and win some coins , gems etc. My problem is that after the game is over i want to start a timer /service/handler (i dont know basically what to say here) of 5 minutes running in background... When the timer is over I Award user his prize.

At present I have used countdown timer and handler with post delayed for that but the problem is that Both countdowntimer and Handler fails to run if user removes the app from recent tasks..the timer is never complete and user never gets his prize. I want to know how can i award the prize after 5 minutes...

A another stackflow user had the similar question How to keep a CountDownTimer running even if the app is closed?. The accepted answer he received https://stackoverflow.com/a/42970754/17072466 . But its 5 year old..i wonder if that will work now due to Oreo+ background process limitations.

Please help me , its above 1 week still i have not resolved it yet.

Avni Krtigya
  • 31
  • 1
  • 4
  • Did you try AlarmManager? You can set the time like 5 minutes. When the time is reached, Broadcast receiver class is called. – SemicolonSpace Oct 27 '21 at 08:05
  • @kumar No kumar ..I don't know how to implement that correctly ..can you share a link for that..I just started learning android so dont know much about all stuffs – Avni Krtigya Oct 27 '21 at 08:07
  • Watch this https://www.youtube.com/watch?v=xSrVWFCtgaE . I'm not sure but if you run a thread even after closing the app, you get a memory leak. So, I suggest you use AlarmManager. – SemicolonSpace Oct 27 '21 at 08:13
  • @kumar ok thnaks for the link ...just implementing this...will let you knw under 30 min if it works – Avni Krtigya Oct 27 '21 at 08:15
  • @kumar the link you sent will work..but it will give a notification everytime user finish playing a game. Suppose user plays 10 games in 15 minutes ..so their will be 15 diffrent notofications.. Any way to hide notification? – Avni Krtigya Oct 27 '21 at 09:24
  • In the video, when the alarm is triggered, onReceive() method is called (which is in AlarmReceiver class). Watch the video at 5:22 time. You can write anything you want in the onReceive() method. No need to display the notification. – SemicolonSpace Oct 27 '21 at 09:36
  • i would like to know..is this the correct way of setting an alarm at a given time... suppose i want to set it at 3:50 PM today ...So we have to do it like this ?? calender = Calendar.getInstance(); calender.set(Calendar.HOUR_OF_DAY,15); calender.set(Calendar.MINUTE,50); calender.set(Calendar.SECOND,0); calender.set(Calendar.MILLISECOND,0); – Avni Krtigya Oct 27 '21 at 10:00
  • @kumar yes it is working. Thanks. Would have upvoted the answer if you have answered rather than comment. But anyways thank u so much. But as this is alarm..is their something available as of now that works as timer in background ? – Avni Krtigya Oct 27 '21 at 10:16
  • Great! You can also upvote the comment. – SemicolonSpace Oct 27 '21 at 10:18

0 Answers0