Android: PendingIntent sends broadcast initially, how to not broadcast initially? - android

The PendingIntent sends a broad cast when the method getBroadcast is called.
AlarmManager alarmManager = (AlarmManager)this.getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(Constants.ALARM_ACTION);
PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, getMidnight()
.getTimeInMillis(), mPendingIntent);
I want the broadcast to only be sent when the alarm goes off not when getBroadcast is called, but the alarm requires a pending intent. Any ideas?

FIGURED IT OUT:
In the API Documentation - If the time occurs in the past, the alarm will be triggered immediately.

Related

AlarmManager cancel() does not stop my Alarm

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.

Canceling alarms from AlarmManager

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();

Alarm preventing activity from finish

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 );

AlarmManager still triggering alarm after canceling the associated service and PendingIntent

I have a service which uses PendinIntent and AlarmManager to launch another activity after a fixed period of time.
Here is the relevant code of the service:
Calendar cal = Calendar.getInstance(); //Create a calendar
cal.add(Calendar.MINUTE, 1); //Add the set minutes to the alarm
Intent dialogIntent = new Intent(this, alarmRingLayout.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1234, dialogIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
When the service starts, I also set up a notification which has the a button that can cancel the service incase the user does not want the activity to launch.
On click of the "cancel" button, stopService() is called:
stopService(new Intent(StopPowerNapAlarmService.this, PowerNapAlarmService.class));
onDestroy() has the following code which cancels the notification and calls stopSelf()
It also tries to cancel the PendingIntent and AlarmManager.
The problem is that the Activity opens up even after after onDestroy is called. I believe the PendingIntent and/or AlarmManager are not getting canceled.
Here is the code for onDestroy():
#Override
public void onDestroy() {
Toast.makeText(this, "Alarm Finished", Toast.LENGTH_SHORT).show();
CancelNotification(this, 0);
//Cancel the pending intent and AlarmManager
Intent myntent = new Intent(PowerNapAlarmService.this, PowerNapAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
1234, myntent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent.cancel();
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);
stopSelf();
}
What is going wrong over here?
What is going wrong over here?
Your Intent objects are different, so your PendingIntent objects are different. This, in turn, means that you are working with different alarms. The first Intent points to an alarmRingLayout.class activity. The second Intent points to a BroadcastReceiver oddly named PowerNapAlarmService.
If you want to cancel() the alarmRingLayout alarm, create an activity PendingIntent for alarmRingLayout, and use that with cancel().
Also, please get rid of stopSelf() in onDestroy(), as that is not needed and could conceivably cause problems.

Canceling alarm manager when service has finished

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);

Categories

Resources