I'm creating the alarm with this code when my widget is added to screen:
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, WidgetAlarmReceiver.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 60000, 60000, alarmIntent);
And I'm deleting the alarm using this code when no more widgets are present on the screen. I'm calling this code on the onReceive method of the AlarmReceiver class. Maybe that is the problem?
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent mIntent = new Intent(context, WidgetAlarmReceiver.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, mIntent, 0);
alarmMgr.cancel(alarmIntent);
The problem is that the alarm is still getting called each minute even doing the cancel call.
What is wrong? The startAlarm code is not getting called anymore so I don't understand why the cancel doesn't works and the alarm gets called again and again.
Finally I solved it, by canceling the alarm from outside the alarm onReceive method itself. Maybe an alarm cannot cancel itself or something.
Related
I want to schedule a repeating alarm at user define time. For that i am using following code
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("id", id);
intent.putExtra("ontime", flag_ontime);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP,
SystemClock.elapsedRealtime() +calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
All works fine...my alarm also fire at desired time but in my receiver i always get only default values from intent not passed one
AlarmReceiver.class
long id = intent.getLongExtra("id", -1);
boolean ontime = intent.getBooleanExtra("ontime", false);
here id is always -1 and ontime is false....please help me
well i for now i have solved this by using alarmmanager setExact() method and on each event fire i re-schedule alarm as per its repeating time....looking for batter solution
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 am using Alarm service in an Android application and here is the code snippet for same
Creation of Pending Intent
Intent intent = new Intent(context, UninstallService.class);
intent.putExtra("APPLICATION_PREFERENCE", applicationPreference);
intent.setAction(intentAction);
PendingIntent pendingIntent = PendingIntent.getService(context,
intentRequestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Scheduling of an Alarm
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
After scheduling the alarm, I invoke finish on Activity, but the activity doesn't finish. It stays open and there is no clue what is happening. If I comment the call to set method on AlarmManager and invoke finish on Activity, the activity is closed.
This is bit weird and I am not sure, what I am missing. Please help.
Android OS : Lollipop (5.0.2 - API 21)
Try this code.
Intent intent = new Intent(context, UninstallService.class);
intent.setAction(intentAction);
intent.putExtra("APPLICATION_PREFERENCE", applicationPreference);
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmMgr .setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent );
I want to cancel my alarm-manager, which starts a service, and which I created using following code:
Intent intent = new Intent(getActivity(), CheckStatusService.class);
pintent = PendingIntent.getService(getActivity(), 0, intent, 0);
cal.add (Calendar.MINUTE,1);
alarm = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60*1000, pintent);
I am doing
alarm.cancel(pintent)
and it is not throwing any errors, but the service I started using this Alarmmanager still continues to work.
Once the service is finished, it sends out a broadcast to one of my activities. There it is definitively received, even alarm.cancel() is called, but the service still continues to run.
What am I doing wrong?
I solved my issue by cancelling the alarm within the service by using the following code:
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), CheckStatusService.class);
PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0, intent, 0);
alarmManager.cancel(pintent);
I am calling a webservice for every 30 seconds using alarm manager.I register it on the time of login in the application. But it is getting called for every 3 seconds. Further, In other activity i am cancelling it but it is not getting cancelled also.
Code for register :
Intent intent = new Intent(this, MyBroadcastReceiver.class);
intent.putExtra("usrid", getIntent().getStringExtra("usrid"));
PendingIntent pendingIntent =PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+2000, 30*1000, pendingIntent);
Toast.makeText(this, "Alarm set in 10 seconds", Toast.LENGTH_LONG).show();
and for canceling it the code is :
Intent intent = new Intent(this, MyBroadcastReceiver.class);
intent.putExtra("usrid", getIntent().getStringExtra("usrid"));
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
What is the problem. Please help. Thanx in advance.