set notification on alarm receiver android
In my android application i would like to set an alaram programmatically. Setting alarm is working properly but how can i apply notification to this on recieving alarm.
I have gone through notifications in developer guide.
Please find the code.
Calendar cal=Calendar.getInstance();
Intent alaram=new Intent(Alarmmanager.this,GroupsCheckAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Alarmmanager.this, 0, alaram,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pendingIntent);
cal.setTimeInMillis(System.currentTimeMillis());
sendBroadcast(alaram,"setalaram");
and in broadcast 开发者_如何学JAVAreceiver
public class GroupsCheckAlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(final Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
}
It is working fine but to set notification i need to set notification manager which works fine only in activity extended class ,how can i use it on receive and notify.
Please share your valuable suggestions.
Thanks in advance :)
I think you can use your context
in onReceive
to find the NotificationManager
and call notify
:
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notifications = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
notifications.notify( ... );
}
If you have a Context
you can do just about anything! :)
You may find useful to just use the BuzzBox SDK to schedule a task and to create notifications. It supports a crontab string to schedule recurring tasks. BuzzBox SDK Website
精彩评论