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);
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
I set my alarm receiver as follow:
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("alarmId", REQUEST_CODE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
time * 1000, pendingIntent);
Then I am canceling it from another class:
Intent myIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)activity.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
But it does not work. Please help me!
Your Intents are not equivalent. The first one uses REQUEST_CODE. The second one uses 0. You need to make these be the same.
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 am trying to cancel an alarm set in a Fragment , from another Fragment, how I can do this?
InserisciOra.java (the Fragment in which I set the alarm)
Intent myIntent = new Intent(getActivity(), MyReceiver.class);
if(PendingIntent.getBroadcast(getActivity(), 0,
myIntent,
PendingIntent.FLAG_NO_CREATE) == null){
pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent,0);
alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Intent myIntent = new Intent(getActivity(), MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent,0);
alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
// cancel the alarm
alarmManager.cancel(pendingIntent);
// delete the PendingIntent
pendingIntent.cancel()
use cancel method to cancel your alarm
Intent myIntent = new Intent(getActivity().,
MyReceiver.class);
PendingIntent pendingIntent
= PendingIntent.getBroadcast(getActivity(),
0, myIntent, 0);
AlarmManager alarmManager
= (AlarmManager)getActivity().getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
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);