How to set alarm when phone boot up in android? - android

I have a alarm function in my application. Alarm will work fine when the phone is ON. Problem is when i switch off or power off the phone(for example i will set alarm for 10.00AM, 10.05, 10.10) and i will switch on by 10.02. I am not getting any alarm notifications and all the alarms are cancelled. I have used Service for getting the alarm and i used permission reciever_boot_complete also. Still i am getting problem. please anybody help me.

1.As far as I know, Android system doesn't support alarms when the power is off. If you set an alarm and turn the phone off, the phone won't boot up when the alarm time comes.
2.When using AlarmManager to schedule an alarm, take care of the alram type in methods:
public void set (int type, long triggerAtTime, PendingIntent operation)
public void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)
You should use ELAPSED_REALTIME_WAKEUPorRTC_WAKEUP, otherwise, when the screen is off, the alarm won't be triggered.
See here:

Related

What will alarm manager do if I change phone's time?

I set multiple alarms and in order to control them I change my phone's time. They will work normally ? What In other words, will alarms collapse if I change my phone's time?
Well it all depends on the type you have passed in the set method of alarm Manger.
if you have used ELAPSED_REALTIME or ELAPSED_REALTIME_WAKEUP, alarm will not trigger
ELAPSED_REALTIME
Added in API level 1
int ELAPSED_REALTIME
Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.
if you have user RTC or RTC_WAKEUP, alarm will be triggered according the device time
RTC
Added in API level 1
int RTC
Alarm time in System.currentTimeMillis() (wall clock time in UTC). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.
I don't know what you mean by collapse. But the alarm is only aware of the device's time and so if you do change your device time, it should work against that instead.

How to prevent triggering alarm when screen is off?

I'm trying to trigger the alarm in a specific interval only when the device is awake, This is the code which I used to achieve this:
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime(),
INTERVAL_ONE_MINUTE,
pi);
Considering documentations about AlarmManager.ELAPSED_REALTIME android is not supposed to trigger the alarm when device screen is off (If I mistake not)
Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up
Contrary to my expectation alarm was triggered by system when the screen is off, Am I missing something here?
Why are you not using
protected void onPause();
to fire there
pi.cancel();
alarmManager.cancel(pi);
When they speak of the device going to sleep, that doesn't mean the screen is off.
See: When does android device go to sleep mode?
See: http://developer.android.com/training/scheduling/wakelock.html
When the device's screen isn't on, it still will be woken up to do things from time to time. To save battery, it'll sleep, but apps will inevitably wake it up to do things.
You could check to see if the screen is on: How can I tell if the screen is on in android?
The documentation you cite is just saying that the alarm won't fire when the device goes to sleep when ELAPSED_REALTIME is used.
Sounds like you want the alarm to fire when the application process is still alive. You might look at using ActivityLicecycleCallbacks:
http://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html
This will let you schedule/cancel the alarm based on the process lifecycle as opposed to a single activity.
You might also look at
Application.onTrimMemory
to see when it gets called by the OS. You might find some use cancelling your alarm there.

Android AlarmManager not working when set for longer time [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
In my app I have tried to set an alarm using this code:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2016);
calendar.set(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 1);
calendar.set(Calendar.MILLISECOND, 1);
Intent intent = new Intent(G.context, AlarmService.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext, 1010, intent, PendingIntent.FLAG_UPDATE_CURRENT);
G.alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
It works when I set the alarm for 1 minute or later, but not when I set the alarm for 30 or 40 minutes later. While I am not on the phone for 30 minutes, after 30 minutes the alarm did not work, and when the phone screen is on the alarm worked...
Any help would be appreciated.
Ok that's because after a long time (let's say more than 5 min) your app is Paused and then the phone is "asleep" with lock screen. When you unlock it then you get your Alarm because it was queued.
To solve this problem, you need to implement a Wake Lock and add it's permission to your manifest file.
According to Google Documentation "A wake lock is a mechanism to indicate that your application needs to have the device stay on."
This is the line you have to add in your manifest:
<uses-permission android:name="android.permission.WAKE_LOCK" />
You'll need to implement this object in your onReceive() method in your BroadcastReceiver. You can follow this tutorial that makes use of all you need.
https://www.javacodegeeks.com/2012/09/android-alarmmanager-tutorial.html
Also, note that you'll keep the device "awake" therefore you'll keep the phone processing affecting the battery life, but still you get to be able to get the alarm even if the screen is locked.
Hope it helps!
Since API 19 set() is treated as inexact and may be delayed. If you really need a precise alarm, you should use setExact() (available since API 19) instead :
Note: Beginning in API 19, the trigger time passed to this method is
treated as inexact: the alarm will not be delivered before this time,
but may be deferred and delivered some time later. The OS will use
this policy in order to "batch" alarms together across the entire
system, minimizing the number of times the device needs to "wake up"
and minimizing battery use. In general, alarms scheduled in the near
future will not be deferred as long as alarms scheduled far in the
future.
This change happen on 19+ device (obviously) but also only if the APK's target API is 19+, so you can
change the target API to 18
or use Build.VERSION.SDK_INT to know which method to use.
Additionnaly when you use a *_WAKE_UP alarm, the alarm manager garantees that the device will be awake long enough to execute the receiver's method, but not the service it may launch :
The Alarm Manager holds a CPU wake lock as long as the alarm
receiver's onReceive() method is executing. This guarantees that the
phone will not sleep until you have finished handling the broadcast.
Once onReceive() returns, the Alarm Manager releases this wake lock.
This means that the phone will in some cases sleep as soon as your
onReceive() method completes. If your alarm receiver called
Context.startService(), it is possible that the phone will sleep
before the requested service is launched.
The support v4 library provides a usefull helper class to handle this case : WakefulBroadcastReceiver
In you case, as you are using a service pending intent, I am not sure what wake garanties apply.
i use service
public class AlarmService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onStart(Intent intent, int startid) {
//This is my Code and set Alarm
stopSelf();
}
}

Turn off AlarmManager when phone goes to sleep mode

I have AlarmManager set up to fire every minute. However, I only need it when phone is active. I want to turn off all updates when phone goes to sleep. I am not sure what the default behavior of AlarmManager and widgets in general is when phone goes to sleep. But I noticed that my battery goes down when in sleep mode. Hence, i believe its the widget thats bringing it down.
So, if AlarmManager and other broadcasts keep firing by default, how can I turn these updates off in sleep mode and then turn them back on when phone resumes active mode?
Use public void setInexactRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation) with RTC as type. Only RTC_WAKEUP and ELAPSED_REALTIME_WAKEUP will wake up devices.
E.g. in my code I use
am.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis() + syncInterval, syncInterval, pendingIntent);

Android Alarm What is the difference between four types of Alarm that AlarmManager provides and when to use what?

I want to know the difference between RTC, RTC_WAKEUP, ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP.
I want to write an alarm application where I will set alarm and close my application and expect for alarm for the time set.
There will be multiple alarms. Right now I am writing for emulator but later will test on device. In emulator, once I set the alarm and close the emulator and restart it, then will it be cleared, as I find with RTC, RTC_WAKEUP and ELAPSED_REALTIME. I am confused. Should I used ELAPSED_REALTIME_WAKEUP? I have not seen any tutorial using ELAPSED_REALTIME_WAKEUP.
please explain.
Thanks.
ELAPSED_REALTIME
Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.
ELAPSED_REALTIME_WAKEUP
Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep), which will wake up the device when it goes off.
RTC
Alarm time in System.currentTimeMillis() (wall clock time in UTC). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.
RTC_WAKEUP
Alarm time in System.currentTimeMillis() (wall clock time in UTC), which will wake up the device when it goes off.
Types of Alarms :
ELAPSED_REALTIME – Fires the pending intent after the specified length of time since device boot. If the device is asleep, it fires when the device is next awake.
ELAPSED_REALTIME_WAKEUP – Fires the pending intent after the specified length of time since device boot. It wakes up the device if it is asleep.
RTC – Fires the pending intent at a specified time. If the device is asleep, it will not be delivered until the next time the device wakes up.
RTC_WAKEUP – Fires the pending intent at a specified time, waking up the device if asleep.
There are two general clock types for alarms: "elapsed real time" and "real time clock" (RTC). Elapsed real time uses the "time since system boot" as a reference, and real time clock uses UTC (wall clock) time. This means that elapsed real time is suited to setting an alarm based on the passage of time (for example, an alarm that fires every 30 seconds) since it isn't affected by time zone/locale. The real time clock type is better suited for alarms that are dependent on current locale.
Source: https://developer.android.com/training/scheduling/alarms.html
From the site you can get the difference between the 4 constanst
Below is example of the setting alarm
Calendar mCalendar = Calendar.getInstance();
mCalendar.add(Calendar.SECOND, 20);
Intent intent_Timer = new Intent(TimerEvents.this, AlarmReceiver.class);
intent_Timer.putExtra("alarm_message", "Drax Rules!!!");
// In reality, you would want to have a static variable for the request
// code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(this, 192837,
intent_Timer, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), sender);
Hope this will be helpful to you

Categories

Resources