I am trying to setup a Alaram below, however sometimes it fires normally and sometimes it does not and it only fires when i "open" the app. so say if alarm was schedule for 6pm, and then when i open my app at 7pm then it will fire. any ideas?
Intent intent = new Intent(AndroidApp.Context, typeof(AlarmHandler));
PendingIntent pendingIntent = PendingIntent.GetBroadcast(AndroidApp.Context, uniqueMessageId, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable);
AlarmManager alarmManager = AndroidApp.Context.GetSystemService(Context.AlarmService) as AlarmManager;
alarmManager.SetExactAndAllowWhileIdle(AlarmType.RtcWakeup, triggerTime, pendingIntent);
Switching alarmManager.SetExactAndAllowWhileIdle to alarmManager.SetAlarmClock Fixed it for me. Hope it can help someone else.
Documentation: https://developer.android.com/reference/android/app/AlarmManager#setAlarmClock(android.app.AlarmManager.AlarmClockInfo,%20android.app.PendingIntent)
Related
I'm developing Reminder application.It works fine until I reboot or forcefully stop.I have used both the actions in Manifest: BOOT_COMPLETED and QUICKBOOT_POWERON
and also added the RECEIVE_BOOT_COMPLETED permission, still, my broadcast receiver is not working. Is there any way to solve this issue and to check that forcefully stop is called or not on android through the program.
Thanks in advance.
You can use AlarmManager to set an alarm to check if you are ok or not.
Here an example to set AlarmManager ;
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr =
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
60 * 1000, alarmIntent);
And in your BroadcastReceiver you need to set your alarm again. You can check your receiver every hour.
So even if your app is forced to stop, you have chance to restart when your alarmmanager checks it.
I am writing some code to fire alarm at a specific time. Everything is working fine and alarm is fired as expected.
But when I clear memory using some Memory Cleaner app, then the alarm is not fired.
I have added permissions and intent-filter for Boot Completion that set my alarm after device boot. But I don't know how to reset my alarm if any Memory Cleaner app cleans the memory?
Code for setting alarm
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
Intent intent = new Intent(context, DailyAlarmService.class);
PendingIntent pIntent = PendingIntent.getService(context, Constants.DAILY_ALARM_REQ_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager manager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5000, pIntent);
Thanks in Advance.
I have the following code which simply runs an alarm manager:
public void runAlarm(){
Intent intent = new Intent(context, MyReceiver.class);
intent.setAction(ACTION_TIMEOUT);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
setTimeOutAlarm(TIMEOUT_MINUTES,alarmIntent);
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setExact(AlarmManager.RTC,
Calendar.getInstance().getTimeInMillis() + 3*60*1000, alarmIntent);
}
It works fine. My question is about the pending intent flag: PendingIntent.FLAG_CANCEL_CURRENT
if i run this method twice does it really cancel out the other pending intent thats already in the alarmManager? how does it know its not to cancel it ?I just want to make sure im not causing any leaks by using this flag. My intent is that i can run this code multiple times and it will keep canceling the previous pendingIntent already sent to the alarmManager instance.
But everytime i run this code the alarm count grows by one when i check with this adb command:
adb shell dumpsys alarm | grep com.your.package
so it seems the alarmManager is not canceled perhaps.
I believe you don't need to cancel the old alarm.. but just update it.
I think you can use: PendingIntent.FLAG_UPDATE_CURRENT
Please, make some tests and let me know if it works.
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.set(Calendar.HOUR_OF_DAY, 1);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context,MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7*24*60*60*1000, pi);
I have this simple pending repeating alarm and it works just fine only when system android alarm is not set up. It doesnt matter on what time alarms are set in system Alarm app, looks like this system app stops all my pending intents. Any idea how to debug this? What can cause this problem ?
I would like to use it in paralel with system alarms.
make sure you have permission
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
For Debugging:
override onNewIntent() in your activity MyClass Activity like this:
#Override
onNewIntent(Intent intent){
android.os.Debug.waitForDebugger();
if(intent.hasExtra("alarm")){ //Put a break point here
Log.d("Alarm fired","yes");
}
}
Create you PendingIntent for firing alarm like this:
Intent intent=new Intent(context,MyClass.class);
intent.putExtra("alarm","yes");
PendingIntent pi = PendingIntent.getService(context, 0, intent,0);
So, when your alarm gets fired, control will come to onNewIntent and wait for you to attach a debugger. That time go to Attach debugger button in android studio and attach debugger to your application. And this is how you will know that you alarm has been fired.
This is my first post here. I have searched all around for this topic, but nothing seems to help. When the code below gets executed, the alarm still goes off, when it should not.. meaning that I cannot cancel the alarm, making my app useless for API's lower than 21. Any idea why? Please help! I should mention that when I try this code using an API22 emulator(nexus one) from Android Studio it works, but when I use it with an API18 emulator(samsung s3) from genymotion it does not.
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 8, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
am.cancel(pendingIntent);
Looks like you're just not giving set() enough time to set the alarm before you're calling cancel().
Put the call to cancel() in a separate code block so that it's not being called immediately after set(), and it should work.