I am new to android, I want to run a task only for a specific duration, how can I do this?
I tried using broadcast receiver and AlarmManager but the broadcastreceiver continuously runs even after specified time.
long timetorun= calendarcurrent.getTimeInMillis() - calendar.getTimeInMillis();
Intent c = new Intent(getApplicationContext(),
Phonecallreceiver.class);
pintent = PendingIntent.getBroadcast(getApplicationContext(), 0, c, 0);
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, timetorun, pintent);
You can stop it by canceling the alarm, using the same PendingIntent like the below:
Intent c = new Intent(getApplicationContext(), Phonecallreceiver.class);
pintent = PendingIntent.getBroadcast(getApplicationContext(), 0, c, 0);
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm .cancel(pintent);
Do not forget to make sure that the PendingIntent is not NULL.
Hope that I helped you.
Related
I am working on application which is get the alarm time from database every 2 AM and then at the specified time the activity shows up, my app works fine, but when I manually change the data/time of the device, previous alarms are triggered and I should prevent to this to happens.
Here is my PendingIntent:
Intent i = new Intent(mContext, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(G.context, _id, i,
PendingIntent.FLAG_UPDATE_CURRENT);
Which each _id is unique and when I make a receiver for date changed event before my alarm cancel method fired previous alarms go off!
Can some body help to solve this problem?
Try this code:
Intent intent = new Intent(this, Mote.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.cancel(pendingIntent);
For cancel the alarm:
Intent intent = new Intent(this, Mote.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Edit
Well you can look for the answer and modify as per your requirements:
How to stop Android AlarmManager when there is a manual system time change?
AlarmManager: how to schedule a daily alarm and deal with time changes
Intent intent1 = new Intent(G.context, DailyAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(G.context,
G.dateTimesetAlarm.getDayOfYear(), intent1, 0);
mAlarmManager.cancel(pendingIntent);
pendingIntent.cancel();
I have scheduled android notifications using alarm manager but I couldn't cancel the notification where I tried to cancel it using this notificationManager.cancelAll(); does not work and then I tried to cancel alarm manager itself, unfortunately, does not work.
Does anyone know how to cancel scheduled notification?
This is how I set alarm manager for notification:
Intent myIntent = new Intent(DirctActivityfinal3.this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(DirctActivityfinal3.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, timeOff9.getTimeInMillis(), pendingIntent);
I tried this code to cancel alarm manager, but not working I still get notified
Intent intent = new Intent(ViewTicketActivity.this, DeleteNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ViewTicketActivity.this,0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Your PendingIntents are not the same.
To cancel an alarm, the pending intent that you created to cancel the alarm with must be the same as the one you created the alarm with.
e,g in your example, You are passing different classes into each.
Intent myIntent = new Intent(DirctActivityfinal3.this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(DirctActivityfinal3.this, 0, myIntent, 0);
is not the same as
Intent intent = new Intent(ViewTicketActivity.this, DeleteNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ViewTicketActivity.this,0, intent, 0);
Complete Sample:
Intent myIntent = new Intent(myContext, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(myContext, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
then either
alarmManager.set(AlarmManager.RTC_WAKEUP, timeOff9.getTimeInMillis(), pendingIntent);
or
alarmManager.cancel(pendingIntent);
Declare Intent, PendingIntent and AlarmManager as global variables
to cancel the scheduled
pendingIntent = PendingIntent.getBroadcast(ViewTicketActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.cancel(pendingIntent);
I have met same issue like this .
Delete alarm from AlarmManager using cancel() - Android
"
I'm trying to create and delete an alarm in two different methods which are both called at different moments in the application. logic.
However when I call AlarmManager's cancel() method, the alarm isn't deleted."
In order to set :
Intent myIntent = new Intent(getApplicationContext(),
SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(),
pendingIntent);
In order to delete :
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(),
SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
But this doesn't remove an alarm registered.
Thanks in advance.
The PendingIntent needs to be created exactly as it was when you start the AlarmManager, and it looks like the main issue is that you're using a different requestCode (zero instead of one).
For a quick fix, this should work:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1, myIntent, 0);
alarmManager.cancel(pendingIntent);
In order to use PendingIntent.FLAG_UPDATE_CURRENT flag, see below:
Setting:
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);
Cancelling:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
Initially for me also not worked,After Seeing many posts i realized that the pending intent to be canceled should be same as the original pending intent that was used to schedule alarm.
The pending intent to be cancelled should have set to same action and same data fields,if any have those were used to set the alarm.After setting the same ACTION and data values though i'm not using them,only cancelled the Alarm.
StartAlarmMethod();
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context,AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, day);
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, seconds);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 , pi);
I am using PendingIntents for multiple Alarms. How can i properly use TAGS, so i can later cancel only the Alarm that i don't want to use anymore?
Right now i'm seting Alarms with the code above. But if i set more than 1, using the code appears bellow i stop all upcoming Alarms. Instead of that i want to be able to identify somehow this PendingIntents and cancel only the ones that are not required.
CancelMethod();
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
To support multiple alarms send a different request code each time you call it in the getBroadcast method. To cancel just send the same request code.
Below code you can loop it within for.
Note: replace i with any value and when cancelling pass the same value to cancel the particular alarm.
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, alarmIntent, 0);
manager.set(AlarmManager.RTC_WAKEUP, 10000, pendingIntent);
To cancel, send the requestcode which you want to cancel,
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, i, intent, 0);
alarmManager.cancel(sender);
i have a service(AlarmService.java) where the alarm is being declared:
Intent intentAlarm = new Intent(this, TimeAlarm.class);
intentAlarm.putExtra("Notif_body", Notif_Body);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,new GregorianCalendar().getTimeInMillis()+5*1000, displayIntent);
and an activity(user_settings.java) from where I want to delete that alarm once a checkbox is unchecked:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intentAlarm = new Intent(this,TimeAlarm.class);
PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, 0);
alarmManager.cancel(displayIntent);
but it's not working my alarm is not being deleted why is that although the pendingintent is the same as it should be!!
intentAlarm.putExtra("Notif_body", Notif_Body);
Put the above line while creating the intentAlarm during cancelling the alarm or remove it from the time when you are setting the alarm. I guess this would work.
My problem was :
instead of adding:
if(bool_activate_reminder==true){
//call the alarm method;
}
I was doing:
if(bool_activate_reminder=true){//with one equal
//call the alarm method;
}
so it was always being accessed... when I fixed it and used the following code in the service:
Intent intentAlarm = new Intent(getApplication(), TimeAlarm.class);
//add the notification body to the intent:
intentAlarm.putExtra("Notif_body", Notif_Body);
// create the object
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//set the alarm for particular time
PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP,new GregorianCalendar().getTimeInMillis()+20*1000, displayIntent);
and in the activity:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intentAlarm = new Intent(getApplication(),TimeAlarm.class);
PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(displayIntent);
it worked just fine..