I’ve got an application that needs to show a notification of certain events in a day, depending on the event.
I know how to do it, if I need a service to stay running in the background, or it will run in the background getting these events on certain days.
private void Notificar(String descricaoEvento){
NotificationManager notifier = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Novo evento no The Point Club", System.currentTimeMillis());
notification.flags |= Notification.DEFAULT_SOUND;
PendingIntent pIntent = PendingIntent.getActivity(this, 0, null, 0);
notification.setLatestEventInfo(this, "Title", descricaoEvento.toString(), pIntent);
notifier.notify(0x007, notification);
}
using this method to call when the right date!
Thanks for listening, I’ll try to explain what I’m doing right: I’m making an application for a club party, and the application will show the next event of the celebrations when I open the application, but what I wish is that a notification is created in android notification bar, on which there is a party, serving as a reminder.
I use this code when I open the activity to list events:
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
int anoAtual= c.get(Calendar.YEAR);
long time = c.getTimeInMillis();
Intent it = new Intent("EXECUTAR_ALARME")
p = PendingIntent.getBroadcast(Eventos.this, 0, it, 0);
AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);
alarme.set(AlarmManager.RTC_WAKEUP, time, p);
the line
Intent it = new Intent("EXECUTAR_ALARME")
will call the class:
public class Alarm extends BroadcastReceiver{ @Override
public void onReceive(Context contexto, Intent intent) {
NotificationManager notifier =(NotificationManager) contexto.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "New event in Club", System.currentTimeMillis());
notification.flags |= Notification.DEFAULT_SOUND;
//Intent i = new Intent(this, Eventos.class);
PendingIntent pIntent = PendingIntent.getActivity(contexto, 0, null, 0);
notification.setLatestEventInfo(contexto, "Club", "Teste", pIntent);
notifier.notify(0x007, notification);
}
}
Now I want to know two things:
1 – How do I leave it running in the background without being open with the application.
2 This code only creates only an alarm, and I need to create multiple.
Thanks for the help.
Use AlarmManager to run a PendingIntent at your specific time. At that time, the intent that it stored in the PendingIntent should likely be a service if you’re planning on creating a Notification to display to the user. If you could form this as a question, I may be able to help more, but it’s unclear what you’re trying to accomplish, and what you’ve managed to accomplish.
Additional Details
In your code, it looks like you’re scheduling the alarm for right now, instead of at some time in the future. I’d suggest the following to create the alarm:
the line
will start the Class ServiceClass:
Answers to your questions:
1 – By setting the alarm for sometime in the future, you’re asking the OS to start your PendingIntent (in your case, send a Broadcast; in my exmaple above, a Service) at the time specified (that’s why you need to set the time with the calendar to sometime in the future!)
2 – Creating multiple alarms is a matter of calling the alarm manager with different PendingIntent s scheduled at the appropriate time (for when you want them to fire). The same is true for Notification s, create multiple ones (with unique ids).