I do not understand why I can not create alarm manager queue. Once the first alarm, the others follow in cascade not meeting times.
Here's the code that will be called when click "Start" button:
final AlarmManager[] alarmManager=new AlarmManager[50];
Integer i =0;
final Calendar calendar = Calendar.getInstance();
public void onClick(View arg0) {
Intent myIntent = new Intent(AndroidAlarmSMS.this,
MyAlarmService.class);
pendingIntent = PendingIntent.getService(AndroidAlarmSMS.this, i,
myIntent, 0);
thatDay.set(dataPick.getYear(), dataPick.getMonth(),dataPick.getDayOfMonth(), timePicker1.getCurrentHour(), timePicker1.getCurrentMinute());
calendar.setTimeInMillis(System.currentTimeMillis());
long diff = (thatDay.getTimeInMillis()- calendar.getTimeInMillis());
int sec = (int) (diff / 1000 % 60);
calendar.add(Calendar.SECOND,sec);//Sec it's the difference between time selected and now
alarmManager[i] = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager[i].set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
i++;
}
You are setting all of your alarms to occur at calendar's time, I'm guessing that you meant to use thatDay:
alarmManager[i].set(AlarmManager.RTC_WAKEUP, thatDay.getTimeInMillis(), pendingIntent);
I have a couple notes:
There is only one AlarmManager, so alarmManager contains up to 50 copies of the same system service.
Your index i is dependent on the users actions, you could have less or more than fifty alarms. You should use an ArrayList and add().
Related
I have two alarm which fires at an interval of 3 min. What will I store in the Room database so that I can know which alarm is firing? I am using TIME_ONE and TIME_TWO to pass a value and detecting in the receiver end. But I think it is bad practice for large number of alarms. It would be better it I can match the id of the alarm that will fire with the stored id in the Room database. Or if I store store the requestcode in the db then how will I get the request code in the onReceive method. I have also tried alarmIntent.putExtra(TIME_ONE, 2); and alarmIntent.putExtra(TIME_ONE, 1); I am not sure if one name can hold two values and work fine. What will be the id of the alarm so that I can get the exact alarm in the onReceive method as it fires? If I pass the requestcode using putExtra should I use one tag (TIME_ONE) or multiple tags(TIME_ONE, TIME_TWO) to detect alarms? I don't want to use if else to check which one throwing 1 so that I can handle. How can I use intent of the onReceive method so that I can know directly which alarm is firing? You can imagine the scenario as two alarms set time to alarm and detect for which alarm the onReceive method is called.
private void setAlarm() {
Intent alarmIntent = new Intent(this, MyAlarmReceiver.class);
alarmIntent.putExtra(TIME_ONE, 1);
pendingIntent = PendingIntent.getBroadcast(this, 1, alarmIntent, 0);
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 60 * 3; // 3 min
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 22);
calendar.set(Calendar.MINUTE, 1);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);// repeat every two min interval
}
private void setSecAlarm() {
Intent alarmIntent = new Intent(this, MyAlarmReceiver.class);
alarmIntent.putExtra(TIME_TWO, 1);
pendingIntent = PendingIntent.getBroadcast(this, 2, alarmIntent, 0);
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 60 * 3; // 3 min
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 22);
calendar.set(Calendar.MINUTE, 3);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);// repeat every two min interval
}
The onReceive method:
Calendar instance = Calendar.getInstance();
#Override
public void onReceive(Context context, Intent intent) {
int hour = instance.get(Calendar.HOUR);
int minute = instance.get(Calendar.MINUTE);
int second = instance.get(Calendar.SECOND);
int which_one = intent.getIntExtra(TIME_ONE,0);
int which_two = intent.getIntExtra(TIME_TWO,0);
if(which_one == 1 && which_two == 0){
Log.d(MY_TAG, hour+":"+minute+":"+second+ " First alarm called");
}else if(which_one == 0 && which_two == 1){
Log.d(MY_TAG, hour+":"+minute+":"+second+" Second alarm called");
}else{
Log.d(MY_TAG, hour+":"+minute+":"+second+ " none of them");
}
}
Late answer, hope you've solved this already of course .
I 'd just pass an id of the database row in the alarm intent.
The Insert Dao will return an Id, use it as the alarm identifier, and in onreceive you directly get the relevant row.
I'm currently trying to make an alarm app that will mute all audio volume at the user-defined day and time. However, the alarm seems not fired at the correct time. I found that the alarm is always fired earlier.
This is my set alarm code:
public static void setAlarm(Context context, String time, int day, int reqCode) {
String[] timeSplit = time.split(":");
Calendar calendar = Calendar.getInstance();
int hour = Integer.parseInt(timeSplit[0]);
int minute = Integer.parseInt(timeSplit[1]);
int days = (day-calendar.get(Calendar.DAY_OF_WEEK) + calendar.get(Calendar.DAY_OF_MONTH));
calendar.set(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
days,
hour,
minute,
0
);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, RuleAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, reqCode, intent, 0);
// Set the alarm to be fired every week on the selected day
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
Toast.makeText(context, "Alarm is set", Toast.LENGTH_SHORT).show();
}
I have tried to change setRepeating() to setInexactRepeating() but nothing changed.
AlarmManger is inexact to save battery by combining wakeup calls.
https://developer.android.com/reference/android/app/AlarmManager.html
Try this
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
The problem is that if you are creating PendingIntent for an alarm for past time it will be fired immediately.If you would like to prevent that happening, then simply do not set alarms with a past trigger time . Use BroadcastReceiver for receiving alarm.
Update :
Check we aren't setting it in the past which would trigger it to fire instantly
if(calendar.getTimeInMillis() < System.currentTimeMillis()) {
calendar.add(Calendar.DAY_OF_YEAR, 7);
}
I finally find out the problem. It lies on the day params. What I pass to day params is my Spinner selected position which starts from zero while the Calendar days starts from one (SUNDAY = 1, MONDAY = 2, so on). So, the alarm will always be set in the past days. I plus one to the day params and it's worked.
This is my piece of code where the startAlarm() function got called.
String ruleStartTime = rule_startTime.getText().toString();
// Spinner index starts from zero,
// to match with the Calendar day indexing, we need to plus it one
int dayIdx = day.getSelectedItemPosition()+1;
Alarm.setAlarm(getApplicationContext(), ruleStartTime,
dayIdx, 0);
I am trying to play a ringtone at exactly at 7 PM everyday but it is playing ringtone immediately after its pending intent is registering broadcast.
I called the service in the foreground on a button click and created pending intent there in onStartCommand:
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
startForeground(FOREGROUND_ID,
buildForegroundNotification("DummyApp"));
c = Calendar.getInstance();
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 60 * 60*24;
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR, 19);
c.set(Calendar.MINUTE,00);
manager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
interval, pendingIntent);
Intent alarmIntent = new Intent(AlarmService.this, DataProcessor.class);
pendingIntent = PendingIntent.getBroadcast(AlarmService.this, 0,
alarmIntent, 0);
return START_STICKY;
}
Now I am playing a ringtone on receiving this broadcast in DataProcessor class the on Receive method of Data Processor class:
#Override
public void onReceive(Context ctx,Intent intent) {
playRIng(ctx);
}
But when I run this code,click the button,service is created but alarm is fired immediately after the AlarmService is called and ringtone is played also.How it is possible because I am giving the exact 7 O clock time when registering broadcast.?
Googled a lot but found the same code only and nothing else.Every code is able to play the ringtone on the time but it also plays the ringtone immediately after the broadcast is registered.
Why you don't use a condition?:
#Override
public void onReceive(Context ctx,Intent intent) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR);
if(hour==19)
{
playRIng(ctx);
}
}
If you ran that code at 9pm you would be telling the AlarmManager that the intent should have been run for the first time 2 hours ago.
You need to check if the Calendar time is behind the current time.
If it is you need to add a day to the calendar so that it first triggers tomorrow at 7pm.
Something like this might work:
c = Calendar.getInstance();
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 60 * 60 * 24;
// Not needed
// c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR, 19);
c.set(Calendar.MINUTE, 00);
// ** Add this **
// Check if the Calendar 7pm is in the past
if (c.getTimeInMillis() < System.currentTimeMillis())
c.add(Calendar.DAY_OF_YEAR, 1); // It is so tell it to run tomorrow instead
manager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), interval, pendingIntent);
Intent alarmIntent = new Intent(AlarmService.this, DataProcessor.class);
pendingIntent = PendingIntent.getBroadcast(AlarmService.this, 0, alarmIntent, 0);
You can try the example at developers website understanding the samplecode available there.May be there is some programming mistake in your code but the sample code available there is working exactly you want.
Scheduling Repeating Alarms
Best way, Play with hour & mintue.
Note: Please check your time formate. In my case it's 24 hr formate
Example:
Calendar c = Calendar.getInstance();
//for 12 hr formate user int hour = c.get(Calendar.HOUR);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
//Log.e("time",""+hour+":"+minute);
if (hour == 10 && minute == 0 ) {
}
Can anybody tell me how I can develop an AlarmManager that will report it self with non specified time intervals?
For example, the first alarm is at 12:00am, the second will be its 1:00am, the third will be 3:00am, the fourth will be 8:00am, the fifth alarm will be the next day, and the sixth alarm will after two days.
Here is the solution that I have got. alarmtTime is a String array with time in Unix time stamp.
for(int i =0; i < alarmtTime.length; i++) {
Intent myIntent = new Intent(MeetMeInvitation.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(MeetMeInvitation.this, i, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.set(AlarmManager.RTC_WAKEUP, Long.parseLong(alarmtTime[i]),
pendingIntent);
}
Ok, let me modify the question and make it much easier. I Hope you'll be able to get me the solution for this. Both the alarms should be scheduled simultaneously, which is not happening here. I am even using unique requestcode for the pending intent. HELP ME please.....
//On click Listener
private OnClickListener mOneShotListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,0, intent, 0);
//I even tried sender = PendingIntent.getBroadcast(AlarmController.this,0, intent, PendinIntent.FLAG_UPDATE_CURRENT);
//the alarm to go off 30 seconds from now.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
//adds 2 minutes to the time
calendar.add(Calendar.MINUTE, 2);
sender = PendingIntent.getBroadcast(AlarmController.this,1, intent,0);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender2);
}
};
OLD Question:
I want to set two alarms at the same time for two different operations to be performed.
For Eg. A user sets alarm at 2'O clock and sets the duration to 15 mins. The first alarm should fire at 2'O clock which performs function1 and the second alarm should fire at 2:15 as the user specified the duration as 15 mins which performs the function2. This operation should be repeated everyday at 2'O clock unless the user changes the time.
I am calling this both functions on button click:
On Click Event
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
..........
..........
new ReminderManager(this).setReminder(mRowId, mCalendar);
new ReminderManager(this).wakeReminder(mRowId, mCalendar, duration);
}
}
The setReminder contains
//sets alarm at 2'O clock
public void setReminder(Long taskId, Calendar when, String duration){
Intent i = new Intent(mContext, OnAlarmReceiver.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId);
PendingIntent pi = PendingIntent.getBroadcast(mContext,(int)System.currentTimeMillis(), i, PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
}
The wakeReminder contains
//adds duration i.e. 15mins
public void wakeReminder(Long taskId, Calendar when, String duration){
Intent i = new Intent(mContext, OnAlarmReceiverWake.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId);
Long d = Long.parseLong(duration);
Long mins = d*60*100;
Long milli = when.getTimeInMillis() + mins;
PendingIntent pi = PendingIntent.getBroadcast(mContext, (int)System.currentTimeMillis(), i, PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, milli, pi);
}
I have noticed that whenever mAlarmManager.set(); is executed successfully the LogCat shows notification like
"enqueueToast pkg=com.jellboi.android.togglemode callback=android.app ITransientNotification$stub$proxy#43c0c5f8 duration=0"
but when I set both the alarms simultaneously the notification is not shown when the mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi); is executed
but the notification is shown when mAlarmManager.set(AlarmManager.RTC_WAKEUP, milli, pi); is executed. Also the notification is set for the original time i.e. 2'O clock and not after adding 15mins to it's duration.
Please help, I tried a lot of ways to call this functions at different places like after the 1st alarm is fired but all in vain.
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,0, intent, 0);
You have problem in this line. If you want to simultaneously fire two alarma then you should do something like this
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,giveUniqueRequestIdsHere, intent, 0);
sender = PendingIntent.getBroadcast(this, (int) System.currentTimeMillis(), i, PendingIntent.FLAG_ONE_SHOT);
Do the same for your PendingIntent.getActivity();
Hi try using this to generate request I was generating my own request code but this line worked for me
final int intent_id = (int) System.currentTimeMillis();
When launch new OneShotAlarm.class, it have to design launch mode. Default setting would ignore the second time launch, if the first instance still existed.
Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
seems like you forgot to set "sender2" before your second call.
you change the value of sender, then put sender2 as the argument.
You can create an array of your task in the Service class and set the array in the for loop, inside the for loop you can call your task simultaneously at the same time.
Try this:
Calendar cal = new GregorianCalendar();
Date date = cal.getTime();
cal.clear();
Cursor c = database.query(mArray.get(mPosition), new String[]{"days"},"days"+"!="+0, null, null, null, null);
cal.set(Calendar.YEAR,2011);
cal.set(Calendar.MONTH,date.getMonth());
cal.set(Calendar.DAY_OF_MONTH,date.getDate());
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(nextScreen.this, AlaramReciver.class);
i.putExtra("Start",mArray.get(mPosition));
Editor medit = getSharedPreferences("Start", 0).edit();
medit.putString("Start", mArray.get(mPosition));medit.commit();
PendingIntent pendingintent = PendingIntent.getBroadcast(this,mPosition, i, 0);
mgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingintent);
You have set to the time like this, and once you set time in alarm manager it will automatically call the functions daily. Use different request code for pendingintent.
If you want call second function. You just add the time in 15 min in the
cal.set(Calender.MINUTE,min+15);