I tried to set repeating AlarmManager using setExact. In activity:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.setAction(ACTION_ALARM_RETRY);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, 7000, sender);`
And in receiver after my operations I have:
public void onReceive(Context context, Intent intent){
//Here are my operations...
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
intent.setAction(ACTION_ALARM_RETRY);
PendingIntent sender = PendingIntent.getBroadcast(context, 1, intet, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, 7000, sender);
}
But it repeats all time without foggiest pause...
Help please!
Related
This is what I am using to set a alarm and it is visible when I fetch all the alarms set in the device.
/**
* Set alarm
**/
Intent alarmIntent = new Intent(context, LocalReminderReceiver.class);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long time = Long.valueOf("1526112720000");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, alarmIntent, FLAG_UPDATE_CURRENT);
manager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
/**
* Cancel alarm
*/
PendingIntent sender = PendingIntent.getBroadcast(context, 1, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
sender.cancel();
Am I missing something?
To set alarm use below line
Intent intent = new Intent(context, AlarmReceiverActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, requestID, intent, 0);
AlarmManager am = (AlarmManager)context.getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
To Cancel alarm use below line
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiverActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,requestID, intent, 0);
am.cancel(pendingIntent);
Above both code for set and cancel alram is working for me.
You have to just pass same request id to set and cancel particular alarm
this is how i am starting a repeating service.
Intent intent2 = new Intent(Main_page.this, SyncService.class);
final AlarmManager alarm2 = (AlarmManager)
Main_page.this.getSystemService(Context.ALARM_SERVICE);
PendingIntent pintent2 = PendingIntent.getService(Main_page.this, 0, intent2, 0);
alarm2.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 2 * 60000, pintent2);
is there any chance of alarm stops repeating.
You can cancel an intent like:
Intent intent = new Intent(context, AlarmService.class);
PendingIntent pIntent = PendingIntent.getService(context, model.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pIntent);
I'm beginner in android,and simple run the alarm manager in mainActivity with this code:
Intent intent = new Intent(testSendWithFood.this, AlarmReciever.class);
intent.putExtra("key", "Alert");
//PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 10, 40 * 1000, pendingIntent);
and i want in another activity kill that alarm manager with this code:
Intent intent = new Intent(this, AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
but so alarm manager not kill!,why?thanks.
The pending intent has to be the same. So if you change the intent id to 1253 when you create the intent and then use the same to cancel the pending intent so change
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),0, intent, 0);
To
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 1253, intent, 0);
Hi I'm trying to cancel AlarmManager. I wrote some test code but It,s not working, my code:
Alarm creation:
Intent myIntent = new Intent(this,NotificationService.class);
PendingIntent pendingIntentFriday = PendingIntent.getService(this,123098,myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,nextFriday.getTimeInMillis() , pendingIntentFriday);
Alarm cancel:
Intent stopIntent = new Intent(this,NotificationService.class);
PendingIntent stopFriday = PendingIntent.getService(this,123098,stopIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager stopManager = (AlarmManager) getSystemService(ALARM_SERVICE);
stopManager.cancel(stopFriday);
Try changing your flag to "FLAG_UPDATE_CURRENT"
AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ReceiverForCancelling.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
service.cancel(pending)
I have created the alarm as shown below
Intent intent = new Intent(this, Areceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 1234567, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, timenow, 3000, sender);
I created a button to stop the alarm. In the onclick method i have written the following code
Intent intentstop = new Intent(this, Areceiver.class);
PendingIntent senderstop = PendingIntent.getBroadcast(this,
0, intentstop, 0);
AlarmManager alarmManagerstop = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManagerstop.cancel(senderstop);
But the job is not stopping. What might be the issue?
You gave your Pending intent a request_code = 1234567 when you start the alarm. But you are using 0 when you try to stop it. Use this when trying to stop and should work
Intent intentstop = new Intent(this, Areceiver.class);
PendingIntent senderstop = PendingIntent.getBroadcast(this,
1234567, intentstop, 0);
AlarmManager alarmManagerstop = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManagerstop.cancel(senderstop);