I am developing an Alarm clock type app which shows notification bar at the time which is set by the user using Time picker widget, for this I use Alarm Manager as follows:
TimePicker tm=(TimePicker)findViewById(R.id.timePicker);
int hour=tm.getHour();
int min=tm.getMinute();
Intent intent = new Intent(this, my_broadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(),234324243, intent, 0);
Calendar cSchedStartCal1 = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));
int h=cSchedStartCal1.HOUR;
int m=cSchedStartCal1.MINUTE;
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,X, pendingIntent);
I want to know the Value of X so to Notify at that time
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));
calendar.setHour(HOUR_OF_DAY, hour);
calendar.setMinute(MINUTE, minute);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Also, your app won't work properly on android marshmallow and later. You should consider using AlarmManager along with setAlarmClock() as described here
then try like this
Calendar calendar = Calendar.getInstance();
calendar.set(year, month,day,
hour, minute, 0);
long startTime = calendar.getTimeInMillis();
here set year as current year, current month, current day, selected hour and minute
Related
I am doing an alarm clock and although using exact alarms it is not starting at the change of minute:
For example I set is at 7:30 it will start somewhere between 7:30:00 - 7:31 (have not been able to check exact time).
I call the alarm like this:
// Create the alarm
CustomAlarmManager alarmManager = new CustomAlarmManager(getActivity());
alarmManager.scheduleRepeatingAlarm(getActivity(), alarmID, alarmHour, alarmMinute);
Which runs this function to create the alarm:
public void scheduleRepeatingAlarm(Context context, int alarmID, int alarmHour, int alarmMinute) {
// Create an intent to go to the notifications reciever class
Intent intent = new Intent(context, AlarmNotificationReciever.class);
// Bundle information and add to the intent for use later
Bundle extras = new Bundle();
extras.putInt("AlarmId", alarmID);
extras.putInt("AlarmHour", alarmHour);
extras.putInt("AlarmMinute", alarmMinute);
intent.putExtras(extras);
// Create a pending intent for the alarm id and intent
PendingIntent pIntent = PendingIntent.getBroadcast(context,
alarmID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
// Create a calender variable with the time for the alarm
Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, alarmHour);
calender.set(Calendar.MINUTE, alarmMinute);
// Create a variable for the current alarm time
LocalTime currentAlarmTime = LocalTime.parse(alarmHour+":"+alarmMinute+":"+"00");
// Create a variable for the local time
LocalTime localDeviceTime = LocalTime.now();
// If the alarm has already elapsed today add one day to it so it runs for tomorrow
if (localDeviceTime.isAfter(currentAlarmTime)) {
calender.add(Calendar.DATE, 1);
System.out.println("The time of day for the alarm has already past rescheduling for tomorrow");
}
// For the different versions of operating system set off a single shot alarm
if(Build.VERSION.SDK_INT >= 23) {
mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
calender.getTimeInMillis(), pIntent);
} else if(Build.VERSION.SDK_INT >= 19) {
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pIntent);
} else {
mAlarmManager.set(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pIntent);
}
}
I am assuming the problem is going to be with these lines here:
Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);
And this line when setting the alarm:
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pIntent);
But I am not sure why that would be not starting exactly on the change of minute.
Thanks for your help
Calendar.getInstance() returns a Calendar instance set to the current time (and date).
You're setting the HOUR_OF_DAY and MINUTE fields of this Calendar instance, but the SECOND field still have its initial value, that is the cause of this behaviour.
Change the following:
Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);
To this:
Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
Alarm Manager does not works properly, as sometimes it triggers the alarm at rigth time but sometimes it triggers alarm after a delayed time of 2-5 minutes and some times it even not triggers the alarm
public static void setAlarmTime(Context context, String userName, String selectedRitual,int dayOfWeek, int hourOfDay, int minute, int rStamp, boolean isUpdate)
{
Calendar now = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
long alarmTime = cal.getTimeInMillis();
Intent intent= new Intent(context, AlarmReceiver.class);
intent.putExtra(AppsConstant.SELECTED_RITUAL, selectedRitual);
intent.putExtra(AppsConstant.user_name, userName);
intent.putExtra("alarmTime", "day"+dayOfWeek+" hour"+hourOfDay+":"+minute);
PendingIntent alarmIntent;
if(!isUpdate)
alarmIntent = PendingIntent.getBroadcast(context.getApplicationContext(), rStamp, intent, 0/*PendingIntent.FLAG_UPDATE_CURRENT*/);
else
alarmIntent = PendingIntent.getBroadcast(context.getApplicationContext(), rStamp, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager=(AlarmManager)context.getSystemService(context.ALARM_SERVICE);
//check whether the time is earlier than current time.
if(cal.before(now))
{
alarmTime = cal.getTimeInMillis()+(alarmManager.INTERVAL_DAY * 7);
}
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, alarmTime, AlarmManager.INTERVAL_DAY * 7, alarmIntent);
}
You're using setInexactRepeating(), which, by documentation, does not always trigger at exactly the time given:
Schedule a repeating alarm that has inexact trigger time requirements;
for example, an alarm that repeats every hour, but not necessarily at
the top of every hour.
If it does not trigger at all when you think it should, consider that you might be rescheduling an existing alarm for the future in some cases.
I want to do a specific task each week .
So I used an Alarm Manager
when user click button alarmManager running
blow code Works properly:
public void set_alarm(int reapte)
{
Context context=getBaseContext();
Calendar calendar=Calendar.getInstance();
// calendar.set(Calendar.HOUR_OF_DAY, 20); // For 1 PM or 2 PM
//calendar.set(Calendar.MINUTE, 41);
Intent intent= new Intent(context, MyService.class);
intent.putExtra("size", reapte);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getService(context, 0,
intent ,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY*7, pi);
}
The problem that I have.
When the user clicks. Alarm Manager runs the same moment.
And next week will run.
I want to first run the alarm from next week
.sorry My language is not good
Try this
you can set the day from which alarm will begin.
calendar.set(Calendar.DAY_OF_WEEK, 1);
where sunday=1 and so on to sat=7
and set the time 2 min before from current time when alarm was set,so it will same day of next week
I finally find the answer
WEEK_OF_MONTH values is between 1 and 4.
so
I use below code:
int week=calendar.get(Calendar.WEEK_OF_MONTH);
int day=calendar.get(Calendar.DAY_OF_WEEK);
int hour=calendar.get(Calendar.WEEK_OF_YEAR);
int minute= calendar.get(Calendar.MINUTE);
calendar.set(Calendar.DAY_OF_WEEK, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE,minute);
int next_week=week+1;
if(next_week>4)
next_week=1;
calendar.set(Calendar.WEEK_OF_MONTH, next_week);
//other code
I need to set alarm in my app to specific day that has been choosen from calendar ? As I have noticed Alarm doesn't have such method. I understnad that I can do this usign calendar and setting the time amount left for this date. But for example if user changes date,what happens ?
Maybe this question is stupid,sorry, I am newbie.
By using the AlarmManager class, you should be able to do this:
http://developer.android.com/reference/android/app/AlarmManager.html
Use public void set (int type, long triggerAtTime, PendingIntent operation) to set the time to fire it.
Use void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation) to schedule a repeating alarm.
Here's how you can do this with your specified (date, hoursOfDay, minute, seconds and even millis):
AlarmManager mAlarmManager = (AlarmMAnager) Context.getSystemService(Context.ALARM_SERVICE);
Calendar mCal = Calendar.getInstance();
Calendar mCalSet = (Calendar) mCal.clone();
mCalSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
mCalSet.set(Calendar.MINUTE, minute);
mCalSet.set(Calendar.SECOND, second);
mCalSet.set(Calendar.MILLISECOND, millis);
if(calSet.compareTo(calNow) <= 0){
//Today Set time passed, count to tomorrow
mCalSet.add(Calendar.DATE, 1);
}
mAlarmManager.set(AlarmManager.RTC_WAKEUP, mCalSet.getTimeInMillis(), yourOperation);
I am using alarm manager to assign the alarm at specific time. I encountered a problem which is if I set the time earlier than the current system time, then the alarm action will be fired instantly. How to prevent this behavior ? Thanks
My code:
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent serviceIntent = new Intent(context,AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(context.getApplicationContext(), 234324243, serviceIntent, 0);
/*sample of setting eariler time
timeAlarm.set(Calendar.YEAR, 2013);
timeAlarm.set(Calendar.MONTH, 10);
timeAlarm.set(Calendar.DAY_OF_MONTH, 13);
timeAlarm.set(Calendar.HOUR_OF_DAY, 12);
timeAlarm.set(Calendar.MINUTE, 9);
timeAlarm.set(Calendar.SECOND, 0);
*/
alarmManager.set(AlarmManager.RTC_WAKEUP, timeAlarm.getTimeInMillis(), pendingIntent);
This is because the android alarm manager is designed by this.
You can modify your time if the time earlier than the current system time by 1 day or more.
like this:
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, second);
if (c.getTimeInMillis() < System.currentTimeMillis()){
c.add(Calendar.DAY_OF_MONTH, 1);
}