How can i set the alarm on the particular date in android? - 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.

Related

Alarm Manager not scheduling alarm properly

I am setting alarm usng this
Calendar now = Calendar.getInstance();
Calendar alarm = Calendar.getInstance();
alarm.set(Calendar.HOUR_OF_DAY,21);
alarm.set(Calendar.MINUTE,30);
if (alarm.before(now)) {
alarm.add(Calendar.DAY_OF_MONTH, 1); //Add 1 day if time selected before now
}
AlarmManager alarmManager =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context,Receiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context,(int)alarm.getTimeInMillis(),i,0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, (int)alarm.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pi);
But even if I schedule it for next day,it triggers immediately after saving alarm.
Dont know what the issue is have searched a lot but everyone else gets it working
You are casting a long timestamp to int thus losing bits and changing the actual timestamp value. You end up with a time that already has passed so it executes the intent immediately.
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
Notice that I removed the (int) cast in the last line.

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);

Run Alarm In Selected Days

I want to start alarm when i have selecting the days,hours,and minutes.
This is my code so far :
public void startAt() {
Intent alarmIntent = new Intent(backgroundApplication.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(backgroundApplication.this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_WEEK,calendar.WEDNESDAY);
calendar.set(Calendar.HOUR_OF_DAY,7);
calendar.set(Calendar.MINUTE,35);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 5, pendingIntent);
}
For the example i set the DAY_OF_WEEK is today(friday) it works great. But when i changed it into Wednesday, or other day before friday it still running.
Is there any other way to start in selected days ?
According to the documentation http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating%28int,%20long,%20long,%20android.app.PendingIntent%29, the 2nd parameter is the trigger time in milliseconds and if you set it to a time in the past, the alarm will trigger immediately. If you want the alarm to only start from next Wednesday, then you have to advance your Calendar time to the next Wednesday with calendar.add(Calendar.WEEK_OF_MONTH, 1). By the way, in your code, you don't need to have calendar.setTimeInMillis(System.currentTimeMillis()).Calendar.getInstance() will return the current time by default. You should also use static field Calendar.WEDNESDAY.

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