How to start an activity on a certain time? - android

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.

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 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.

Overwrite existing alarm

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.

Android: AlarmManager to trigger an event every day

Im trying to make an app, that would remind me of an event at the same time every day. For this purpouse Im using an AlarmManager.
This is how I register an event:
Intent alarmIntent = new Intent(getApplicationContext(), ReminderAlarm.class);
alarmIntent.putExtra("id_bundle", cursor.getInt(0));
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), cursor.getInt(0), alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
This is how I unregister an event:
Intent alarmIntent = new Intent(getApplicationContext(), ReminderAlarm.class);
alarmIntent.putExtra("id_bundle", id);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), id, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pendingIntent);
This is how I edit an event:
Intent alarmIntent = new Intent(getApplicationContext(), ReminderAlarm.class);
alarmIntent.putExtra("id_bundle", id);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), id, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pendingIntent);
Intent alarmIntentNew = new Intent(getApplicationContext(), ReminderAlarm.class);
alarmIntentNew.putExtra("id_bundle", id);
PendingIntent pendingIntentNew = PendingIntent.getActivity(getApplicationContext(), id, alarmIntentNew, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntentNew);
I tried using setRepeating with an interval 24*3600*1000 but that didnt seem to work, so I decided to unregister the event when it triggers and register it again. This I do in the ReminderAlarm activity that starts after the alarm triggers.
Also, whenever I start the application, I register all the events again using the block of code I posted to register an event. I pull data about what events and what times to register them from a database.
My problem is that this doesent work either. When I add an event to a time T+1 minute, it triggers correctly. But the next day, it doesent.
Could it be perhaps because I give the calendar instance wrong data? I have quite a long function that sets up the calendar instance after an event triggers and before I send this calendar instance into the AlarmManager instance. In this function I chech for things like new month, new year etc and set calendar fields (year, month, day_of_month, hour_of_day, minute). Is this even necesary? Does calendar.getTimeInMillis() consider all those fields or just hour_of_day and minute? Do you see what might be wrong? Thank you!
Why not do this to get the time.
Calendar cal = Calendar.getInstance();
int now = cal.getTimeInMillis();
cal.add(Calendar.DAY_OF_MONTH, 1);
int tomorrow = cal.getTimeInMillis();
I think the calender could be what's doing it.
PS. I was the person who suggested the alarm in the last post :P Looks like you are doing good.
That useful code:
//Create a calendar.
Calendar cal = Calendar.getInstance();
//Get the time in miilis since Jan 1st 1970 I think?
cal.getTimeInMillis();
//Set the calendar
cal.set(year, month, day, hour, minute, second);
//set a certain aspect of the calendar
cal.set(Calendar.<What you want to set>, <amount>);
//for example
cal.set(Calendar.DAY_OF_MONTH, 1);
//Add to the calendar, Calendar.DAY_OF_MONTH what ever you are using specifies the amount of milliseconds to add, for example a day is 100*60*60*24 milliseconds where a second is 1000 milliseconds.
cal.add(Calendar.<Day or month or what ever>, <amount>);
//for example
cal.add(Calendar.MONTH, 1);
//Take of the calendar
cal.add(Calendar.MONTH, -1);
//Get that value
int day_of_month = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR_OF_DAY);
Hope you enjoy :)

Categories

Resources