Friday, June 16, 2017

Android: Alarm Manger with broadcast receiver

Welcome to this post. In this post, we are going to learn about Alarm manager and BroadCast Receiver. As usual before start please check official documentation.
1.Alarm Manager official documentation
2.BroadCast Receiver official documentation

Alarm manager is used for scheduling task. If you want to do a task after some time intervals or if want to do some task at 11 am. of the daytime, you have to use alarm manager. Because there is a possibility that user is not using this app at that particular time. So If you are using alarm manager and schedule some task then you don't have to tense about your task is executed or not. More accurately, when phone time is 11.am (example time) then system send a broadcast and from the broadcast receiver, you can receive this signal and execute the task.

Ok, enough talk.

First Alarm Manager-

* we have to create a calendar variable like-
Calendar calendar = Calendar.getInstance();
Now set a time when you do the task. In this case, I want to set 23.59.00 this time. you can also set a specific date. But I don't need a date I just need time so I set time only. But you can also add a date in similar ways.
calendar.set(Calendar.HOUR_OF_DAY,23);
calendar.set(Calendar.MINUTE,59);
calendar.set(Calendar.SECOND,0);

Now we have to do-
 * create an alarm manager and get system alarm services
* create an Intent and Intent class must be a broadcast receiver extend the class (we will create in later in this post).
* set an intent action (so we can filter on broadcast receiver, in this case, I use package name)
* now create a pending intent and get broadcast receiver
*now set alarm manager with set repeating
see the code-
//alarm manager
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(this, TaskManager.class);
intent.setAction("com.blogspot.shudiptotrafder.lifeschedular");

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent,

PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.setRepeating(AlarmManager.RTC_W
  AKEUP, calendar.getTimeInMillis(),
  AlarmManager.INTERVAL_DAY, pendingIntent);
Note: here I set interval time AlarmManager.INTERVAL_DAY so it will be trigger at once in every day. you can set a custom time limit.
That's it on alarm manager.

BroadCast Reciever 

the broadcast receiver receives system and custom broadcast. we also explain in the first section of this post. Create a class extend with the broadcast receiver
public class TaskManager extends BroadcastReceiver {
}
you have to override onRecive method.
@Override
public void onReceive(Context context, Intent intent) {
}
we check that if intent has the same action that we have passed through pending intent on alarm manager. If the answer is true then we do our task. For simplicity, I show a toast message.
if (intent.getAction().equalsIgnoreCase("com.blogspot.shudiptotrafder.lifeschedular")) {

  Toast.makeText(context, "Received", Toast.LENGTH_SHORT).show();
}
Now we have to declare this receiver on manifest (AndroidMenifest.xml)
<!-- Broadcast Receiver -->
<receiver
  android:name=".manager.TaskManager"
  android:enabled="true"
  android:exported="false">
  <intent-filter>
  <action android:name="com.blogspot.shudiptotrafder.lifeschedular"/>

  <category android:name="android.intent.category.DEFAULT"/>
  </intent-filter>
</receiver>
we set the specific broadcast receiver on the intent filter. If you want to receive more action then set on action tag. Full receiver class-
public class TaskManager extends BroadcastReceiver {
  
  @Override
  public void onReceive(Context context, Intent intent) {

  if (intent.getAction().equalsIgnoreCase("com.blogspot.shudiptotrafder.lifeschedular")) {

  Toast.makeText(context, "Received", Toast.LENGTH_SHORT).show();
}
That's all.
Happy coding.

About Author:

I am Shudipto Trafder,
Since 2015, I write android code. I love to implement new ideas. I use java and also kotlin. Now I am learning Flutter. I love to learn new things and also love to share. And that is the main reason to run this blog.


Let's Get Connected: Facebook Twitter Linkedin Github

No comments :