Overwrite existing alarm - android

I create an alaram in my app which is calling a BroadcastReceiver to setup notifications ever day with this code:
Intent intent = new Intent(Benachrichtigung.CUSTOM_INTENT);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alram = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alram.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), (24 * 60 * 60 * 1000), pendingIntent);
Now I want that the user can set the time for the norification so I have to call calendar.set with the new value. How can I overwrite the existing alarm with a new one?

To cancel or update an alarm using AlarmManager the Intent must match using the filterEquals. So basically, you re-create the PendingIntent the same as you did for the original and AlarmManager will see that they are the same. This includes the REQUEST_CODE, INTENT_ACTION, and INTENT_DATA (I may be missing something there but those are important.
Note:
EXTRAS are not used in comparing the two Intents.
So if the two Intents are equal then the first will be overwritten. When I have more time I can try and find a resource to better explain that.
According to the Intent Docs filterEquals
Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

Related

How can i set the alarm on the particular date in android?

Explanation:
I have calendar in which i set the events on the particular day of the given month.The event is stored into the database. on the event day is occurs it is trigger an alarm to notice the user.
suppose, my event is save on the 29/05/2016 then my alarm is triggered on the particular date.
Notice:i have multiple event created on the particular month.e.g. on the 29th may or 30th may also.
MyQuestion is how can i fire the multiple alarm on the particular multiple days.
Please, understand the flow what i exactly want?
If you want to play alarm for particular date and time , following code may help you
Calendar cal=Calendar.getInstance();
cal.set(Calendar.MONTH,5);
cal.set(Calendar.YEAR,2012);
cal.set(Calendar.DAY_OF_MONTH,11);
cal.set(Calendar.HOUR_OF_DAY,16);
cal.set(Calendar.MINUTE,10);
cal.set(Calendar.SECOND,0);
Intent _myIntent = new Intent(getApplicationContext(), ReceiverClass.class);
PendingIntent _myPendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 123, _myIntent,
PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager myAlarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
//myAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), _myPendingIntent);
myAlarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), _myPendingIntent);
calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,date.getYear());
calendar.set(Calendar.MONTH,date.getMonth()+1);
calendar.set(Calendar.DAY_OF_MONTH,date.getDate());
calendar.set(Calendar.HOUR_OF_DAY,time.getHours());
calendar.set(Calendar.MINUTE,time.getMinutes());
calendar.set(Calendar.SECOND,time.getSeconds());
Intent myIntent = new Intent(context, AlarmReciever.class);
myIntent.putExtra("ReminderDetails",reminderDetails);
myIntent.putExtra("requestCode", requestCode);
pendingIntent = PendingIntent.getBroadcast(context, (int) (requestCode), myIntent,0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, date.getTime(),
interval, pendingIntent);
The following code here is good live for setting a calender time at the specified time and date.
And the most important thing here stands is that what do you pass in 2nd argument in alarmManager.setInexactRepeating(_) method,which is
date.getTime(), which is the only thing will work instead of using
calendar.getTimeinMillis() method, because that will return some unexpected time to be launched for intents.

Show repeating Notification from some start Date until some end date

I have many weekly-repeating events every one has a start/End dates.
it should appear every week at the same day & time.
So I want to show notification for every event on every week at a specific date&time starting from it's start Date until it's end date.
Can any one provide me with a tutorial / explanation / Links on how to implement this, please?
Take a look at the official Android docs - link here.
You need to use the setRepeating function in AlarmManager to achieve this. Create a BroadcastReceiver that creates a notification when an Intent is received. Set this Intent in a PendingIntent and give the PendingIntent to the AlarmManager#setRepeating.
Example code -
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);

Android, how to set alarm service to the predefined date time and run notification

I would like to add into my application background service which takes given datetime and ID pointing to the table row with additional informations and in given time displays status Bar notification.
Could somebody give me a simple example how can I do it?
Many Thanks for any advice.
Have you looked at using AlarmManager to schedule whatever process you wanted to do? (https://developer.android.com/training/scheduling/alarms.html)
UPDATE: pass parameter to the intent
For example (copied):
// create Intent
PendingIntent alarmIntent;
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("ID", "ABX1000"); // your ID
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
And in your receiver, get the intent....
#Override
public void onReceive(Context context, Intent intent) {
// Implement code here to be performed when
// broadcast is detected
String myID= intent.getStringExtra("ID");
}
You can trigger a notification or intent using pending intent.
example is here

Android alarm manager with fixed date and repetitive

How to create a AlarmManger which can be invoke on fixed date and time, This can be also repeat continuous by nature
unfortunately any of the options on the AlarmManager for repeating tasks doesn't allow such a fine control. Your best approach is to on every alarm, you re-schedule it for the next month.
PendingIntent pendingIntent = // set here your action
Calendar calendar = // set this guy to be the next 5th day
AlarmManager am = // get reference to the manager
am.set(RTC, calendar.getTimeInMillis(), pendingIntent);
on inside this Pending intent action you repeat the code. For example, let's say you want to launch a BroadcastReceiver
onReceive(Context context, Intent intent){
Calendar calendar = // set this guy to be the next 5th day
AlarmManager am = // get reference to the manager
am.set(RTC, calendar.getTimeInMillis(), pendingIntent);
}
to set the calendar object is easy:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY, 5);
I just read a good answer to do the same.
The code is-
Calendar cal=Calendar.getInstance();
cal.set(Calendar.MONTH,5);
cal.set(Calendar.YEAR,2012);
cal.set(Calendar.DAY_OF_MONTH,11);
cal.set(Calendar.HOUR_OF_DAY,16);
cal.set(Calendar.MINUTE,10);
cal.set(Calendar.SECOND,0);
Intent _myIntent = new Intent(getApplicationContext(), ReceiverClass.class);
PendingIntent _myPendingIntent = PendingIntent.
getBroadcast(getApplicationContext(), 123,
_myIntent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager _myAlarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
//_myAlarmManager.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (10 * 1000), _myPendingIntent);
_myAlarmManager.set(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), _myPendingIntent);
This is explained in android-alarm-setting-with-specific-date.

How to start an activity on a certain time?

This question might be related to this and this question, but unlike those question I want to start them on a specific time, (say 11:12:13 on 15/03/2014).
I am actually working on a project (SMS) that sends messages on a given time.
try this code, I tested it:
First: Create a calendar:
Calendar calendar = Calendar.getInstance();
And set time by one of this ways:
calendar.set(year, month, day, hourOfDay, minute);
// be careful month start form "0" so for "May" put "4"
or:
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
Second: Add this:
Intent intent = new Intent(MainActivity.this, NextActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
((AlarmManager) getSystemService(ALARM_SERVICE)).set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Note: If you need a timer, you can replace alendar.getTimeInMillis() to System.currentTimeMillis() + x in above code. (x is an int of milliseconds that you want make wait)
Use AlarmManager from the Android API. Just follow this tutorial:
http://android-er.blogspot.in/2011/05/using-alarmmanager-to-start-scheduled.html.
In the above tutorial, they talk about scheduling the activity after some interval. In your case, just take the difference between the current time and your time to launch and explore the API to get what you want.
If you want to start Activity on a cretaion time use AlarmManager.
I write a example for this.
Intent intent = new Intent(this, YourReceiver.class);
//11:12:13 on 15/03/2014
Calendar cal=Calendar.getInstance();
cal.set(Calendar.MONTH,2);//because is started from 0
cal.set(Calendar.YEAR,2014);
cal.set(Calendar.DAY_OF_MONTH,15);
cal.set(Calendar.HOUR_OF_DAY,11);
cal.set(Calendar.MINUTE,12);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pendingIntent );
inside your receiver call to activity using Contect.startActivity method.
Don't forget to register receiver inside your meanifest.xml.

Categories

Resources