Scheduling a Background Task in Android?
Tags: Mobile App Development, Java, Android App Development, Android, Broadcastreceiver

When you are developing an android app, there will be numerous cases when you need to send a notification or trigger to the user’s smartphone. Or just perform an action. But, not so surprisingly your app will not be running even in the background.

So how do you perform an action even when your app is closed in the user’s smartphone ?

I spent hours searching for a simple & clear solution when I was working on EazyPG’s Manager app. I needed to send the opt-in/opt-out notification for daily meals to users of our EazyPG Tenant app. I didn’t find any clear solution. So I’ll try explaining it from scratch using the Alarm Manager and Broadcast Receiver.

To show how to do this, I have created a simple application with an enable/disable button.

I will show how to enable it at a particular time and repeating it as per your requirements.

Step 1— Creating an action on button click

Now, we will set the task which is performed on clicking the button.

This button will disable itself when we click on it. And we will enable it using a Broadcast Receiver. The value of SharedPreferences is set to “false” and will be changed to “true” when we send the intent to BroadcastReceiver in the following steps.

Step 2— Creating a Broadcast Receiver

A broadcast receiver is an Android component which allows you to send or receive Android system or application events.

Create a class Receive.java which extends BroadcastReceiver. We will use SharedPreferences to update the trigger which will allow the button to enable again at a given time.

When the intent is received in the onReceive method, the value of key ‘buttonTrigger’ will change to true. It means the button should be enabled now.

Step 3— Sending Intent to Broadcast Receiver

In the MainActivity.java, create an instance of Calendar class and AlarmManager in the onCreate Method.

Calendar class helps us to set the time and AlarmManager helps in performing the task every day at the same time.

This will alert BroadcastReceiver every day at midnight to set the value of SharedPreferences to ‘true’. We will check this value in the following steps.

Step 4— Checking the value of Shared Preferences

Now, every time we open the MainActivity.java in the application, we will check the value of SharedPreference. If the value is ‘true’, we have to enable the ‘clickButton’ otherwise the button will remain disabled. In the onCreate method, write the following code to check the value of ‘buttonTrigger’.

Step 5— Register Broadcast Receiver in Manifest

To make your BroadcastReceiver work, you need to register it in the AndroidManifest.xml file under the application tag, as follows.

And we are done. Now you can run the application successfully.

Here is the complete code for MainActivity.java

Conclusion

Hopefully, this article has highlighted the use of BroadcastReceiver and AlarmManager. It is quite useful when you want to get data from the user only once a day.

Pro Tip: You should always limit the number of BroadcastReceiver in your application as more BroadcastReceivers lower the application’s performance.

If this article has helped you, press the clap button for 5 seconds!