i have created a dietchart arrayList with specific timeperiod, now i want to create a reminder which will notify when it is time for a specific diet with sound.
Although this question is little bit old but it can still help to other users. You have to use alarm manager for triggering at specific time.
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP,<here set time when you want to trigger>,<here you can set time to repeat after trigering continusosly>,pintent);
Related
I made one application with Android native. I'm using AlarmManager for achieving every 1-minute track data.
AlarmManager alarmManager=(AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),100000,pendingIntent);
But I want that it must stop on specific time like today 12 P.M. or whatever time I want.
How can I achieve it by making another AlarmManager Service, or is there any other option for this?
Just check in service launched by Alarm that current time early than your specific time
if (isTimeExpired) {
AlarmManager alarmManager=(AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
alarmManager.cancel(pendingIntent);
}
I am actually writing a reminder android application. I am able to create reminders using the alarm manager and pending intents, but I am not sure how to postpone them.
Here is how I create a reminder...
AlarmManager mgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
Inent notificationIntent = new Intent(c, ReminderAlert.class);
notificationIntent.putExtra("REM_NAME",remName );
PendingIntent pi = PendingIntent.getBroadcast(c, a unique id for the reminder, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mgr.set(AlarmManager.RTC_WAKEUP,remTime, pi);
I am able to get the intent used in the pending intent...
How would I postpone the alarm manager and pending intent to a new time (as a long)?
Set a brand new alarm for the new time.
I am setting alarms at diffrent time . I want to delete a particulat alarm.
Ex. I set 100 alarms at diiferents times , now I want to Delete an alarm set at 25 Feb 2012 10.45 AM. How Can I do that.
I written following code to set alarm.
final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
GregorianCalendar gc=new GregorianCalendar();
gc.set(2012, 1, 22, 10, 42,0);
Intent intent = new Intent(this, AlarmService.class);
gc.set(Calendar.AM_PM,0);
final PendingIntent sender = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, gc.getTimeInMillis(), sender);
I have a broadcast receiver to recieve the alarm.
You need to save the request code of alarm,that is 1 in your case here. The 2nd parameter you pass in your PendingIntent.getBroadcast() method is your request code for a particular alarm.So when you save request code along with the time for what you set alarm,it would be easy for you to get the particular alarm instance and then you can cancel it using:
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, OneShotAlarm.class); //OneShotAlarm is the broadcast receiver you use for alarm
PendingIntent sender = PendingIntent.getBroadcast(context,alarm_request_code, intent, 0);
am.cancel(sender);
You should take care that each of your request code for different alarms should be unique,otherwise your alarm will be rescheduled with new time you set with the older request code.
Hope you get the point.
You might need to store it in some form of data structure to keep track of the time and the object. That being said, I have not tried storing AlarmManager objects before.
This is what sets the alarm
public void setSilent(Long taskId, Calendar when){
Intent i = new Intent(mContext, SilentReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(mContext, 1 , i, PendingIntent.FLAG_ONE_SHOT);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
It takes in an id and date to use with the intent.
I am canceling it in another file when the user clicks delete. using
Intent i = new Intent(null, SilentReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(null, 1 , i, PendingIntent.FLAG_ONE_SHOT);
should this work because it has the same request code as the first one or am I doing something wrong?
You have to save a reference to your first PendingIntent. This way you can use:
mAlarmManager.cancel(pi);
And the alarm should be cancelled.
This answer has been repeated many times. Please search before asking.
Be careful to send a PendingIntent to stop the alarm with the same data than the Intent to create the alarm: problem with cancel the alarm manager pending intent
I am trying to get a reminder/alarm Service` working in my note/todo app. I am able to set a reminder for a particular item and the alarm triggers and displays a notification successfully.
My problem is how can my Service know which note/todo item set that particular reminder. I'd like for the user to be able to click on the notificaiton in the status bar and have the item which trigger it come up. But I have no way of passing that information to the Service as they don't accept Bundles from the PendingIntent.
I currently set the alarm with the following:
private void createAlarm() {
Intent i = new Intent(this, AlarmService.class);
PendingIntent sender = PendingIntent.getService(this, 0, i, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, mReminderCal.getTimeInMillis(), sender);
}
I just need a way to send along the _id of the item in my database so my Service can launch the item with that same _id when the notification is clicked.
I hope my question isn't too confusing.
Thanks!
Why don't you put all your need into Intent data? Something like this:
final Intent intent = new Intent(context, UpdatesActivity.class);
intent.putExtra(ID, "foo");
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
Then on receiving end you do
String id = getIntent().getStringExtra(ID);