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();
Related
I do have a NoteApp, where you can set reminder.
I want to implement that you can cancel the alarm.
My problem is I cant find any tutorial how to do this.
I've set an unique ID and saved it into the SQLite Database.
So when I click on Dialog "Yes" it should cancel the Alarm.
Here I set the alarm:
Intent i = new Intent(NeueNotiz.this,NoteNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(NeueNotiz.this, (int) System.currentTimeMillis(),i,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
isAlarmSet(true,String.valueOf(cal_new.getTimeInMillis()));
alarmManager.setExact(AlarmManager.RTC_WAKEUP,cal_new.getTimeInMillis(),pendingIntent);
I have absolutely no clue.
Help please
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);
See How to cancel alarm from AlarmManager link for more info.
I made one application with Android native. I'm using AlarmManager for achieving every 1-minute track data.
AlarmManager alarmManager=(AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),100000,pendingIntent);
But I want that it must stop on specific time like today 12 P.M. or whatever time I want.
How can I achieve it by making another AlarmManager Service, or is there any other option for this?
Just check in service launched by Alarm that current time early than your specific time
if (isTimeExpired) {
AlarmManager alarmManager=(AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
alarmManager.cancel(pendingIntent);
}
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 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.
I'm using this code (from my activity) to start a periodic timer in the application:
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),(i * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",
Toast.LENGTH_SHORT).show();
I want to know how can i know (from a different activity) is the timer is running?
One possibility I can think of is to check if the pending Intent is defined.
You can call
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_NO_CREATE);
this will give you the pending Intent, or null if the Intent was not specified. This will not guarantee that the alarm was set but it is an easy way to check if the set alarm code was called before.
Another possibility is to save all set alarms in a SharedPreferences file. In this way you can look for all alarms set by your app in the shared prefs file.
As far as I can see there is no way to get a list of set alarms for your application from the AlarmManager
in stop function
Intent intent = new Intent(this, LocaitonSampleBroadcaseReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast( this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.cancel(pendingIntent);
just add this:
pendingIntent.cancel();
so you can use the string wrote comment Janusz
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_NO_CREATE);