0

I am in the process of making an app that will be triggered by a system broadcast and would take time input from the user, after which the app should just run a timer and do a task after the timer is over. My questions are as follows:

  1. Do I need to use a background task for this, or is this possible to be achieved without it, because I need the timer to run even if the app is closed in the app drawer.
  2. If I do need to use a background task, what should I use, an AsyncTask, a Service or a JobScheduler?

I understand that a BroadcastReceiver will listen to any system broadcasts, and since I have registered in the Manifest, the app will start on any such system broadcasts. However, as soon as the app is started due to the system change, I need it to popup a dialog box which takes input in the form of time (HH:MM:SS), and after that a timer begins which runs for that amount of time and as soon as the timer stops, another task is done.

I also don't want the task to be a one-up, meaning that I want it to be to done every time the system receives the system change broadcast.

Aqib Ahmed
  • 48
  • 8

1 Answers1

1

For what you want to do, you basically need three things.

  1. A BroadcastReceiver for receiving a system broadcast. This component is essential. Also, you don't have to worry about it being a one-up thing. A registered BroadcastReceiver will continue to run it's onReceive() method until the BroadcastReceiver is unregistered by you or the system, or if you intentionally place code in to block it from activating.

  2. An Activity to display the Dialog. Technically, a BroadcastReceiver can also display a Dialog, but BroadcastReceivers are meant for short and quick tasks, so it's not a good place for this. An Activity where you show a DialogFragment is the better option because compared to a Service, an Activity is really the component meant to display a UI.

  3. An AlarmManager for counting down the time. Rather than creating a Service yourself to handle the timer, you should use the AlarmManager with exact time to help you respond to the amount of time that passed. You can also use a JobScheduler as an alternative to AlarmManager, since both are meant for executing code at a later time. Which one you choose depends on the task you want to do later on. Personally, you should also consider the new WorkManager, which is the better option in my opinion. Depending on what you need to do, it will internally use a JobScheduler or AlarmManager, which helps get rid of the deciding process for you.

Jackey
  • 3,184
  • 1
  • 11
  • 12
  • So do I have to specifically register a broadcastreceiver that I have registered in the manifest again in the mainactivity? I mean on the developer docs, it doesn’t say anything about registering the receiver anywhere except the manifest. – Aqib Ahmed Jul 12 '18 at 02:53
  • @AqibAhmed For all BroadcastReceivers, you can choose to register it through the Manifest or through Code. Once successfully registered, you won't have to register it again in the other option. The only exception to this are some system broadcasts such as ACTION_SCREEN_ON which cannot be registered in Manifest. So in your case, as long as your BroadcastReceiver can receive the broadcast through Manifest, you won't have to worry about registering it again anywhere else. – Jackey Jul 12 '18 at 03:17
  • Thanks Jackey, you’re absolutely amazing. – Aqib Ahmed Jul 12 '18 at 03:26
  • I know it’s weird to ask, but I often get some really dumb doubts, could you please email me at insignificanthomosapien@gmail.com if you’re okay with me sending you some small queries on email? – Aqib Ahmed Jul 12 '18 at 03:27