Alarm Manager doesn't work on time - android

I have this method which should schedule alarms but when the time arrives it doesn't start the pendingintent ??
public void setAlarm(String name, long time) {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent dialog = new Intent(this, SubActivity.class);
dialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(this, 0, dialog, 0);
if (Build.VERSION.SDK_INT >= 19) {
if (System.currentTimeMillis() < time) {
am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, pi);
}else{
time+=(AlarmManager.INTERVAL_DAY*7);
am.setExact(AlarmManager.RTC_WAKEUP, time, pi);
}
} else {
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, AlarmManager.INTERVAL_DAY * 7, pi);
}
}

The problem is you are using a PendingIntent for an Activity, which will not necessarily keep the device awake long enough for the Activity to get started. You'll have to use a PendingIntent for a BroadcastReceiver which leverages a wake lock to keep the device awake until your app code can run. WakefulBrodcastReceiver is a good choice, or you can roll your own as needed. See this article for an explanation and sample of how to use alarms to wake the device: http://po.st/7UpipA

Related

alarm manager not working while updating interval time

after reading all the QA i didnt get any proper solution.
I have 2 problems
1. Alarm fires twice even if i register my receiver only in manifest.(not by code)
2. when i update interval time of alarm it gets fires randomly
here is my method for set alarm
public void AlarmCall(int min) {
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pintent = PendingIntent.getBroadcast(context,0 , intent, 0);
alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
cancelAlarm(alarm,pintent);
if(Build.VERSION.SDK_INT<18) {
alarm.set(AlarmManager.RTC_WAKEUP, 1000 * 60 * min, pintent);
}
else if(Build.VERSION.SDK_INT>=19 && Build.VERSION.SDK_INT<=22)
{ alarm.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), 1000*60*min, pintent);
}
else if(Build.VERSION.SDK_INT>=23)
{ alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,1000*60*min,pintent);
}
}
method to cancel alarm :
public void cancelAlarm(AlarmManager alarm,PendingIntent p)
{
alarm.cancel(p);
Log.d("Alarm","Alarm Cancle");
}
in my project Application class i have to start alarm with 10 min time interval and it works fine , according to user input value i need to update time interval.
so i call this method with int min input value and cancel first alarm.
but in marshmallow it fires at every 5 seconds, and kitkat lollipop it fires randmoly.
even checked with setExact() method
I had the same issue, use setWindow solved the problem
long repeatInterval = 1000 * 60 * min;
long triggerTime = SystemClock.elapsedRealtime()
+ repeatInterval;
AlarmManager alarms = (AlarmManager) this.getSystemService(
Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= 19)
{
alarms.setWindow(AlarmManager.RTC_WAKEUP,
triggerTime,
repeatInterval,
pendingIntent);
}else{
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
triggerTime,
repeatInterval,
pendingIntent);}

AlarmManager alert after defined time

Below is my code
public static void startAlarmManager(Context context, Date date) {
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
alarmIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
PendingIntent recurringAlarm = PendingIntent.getBroadcast(context.getApplicationContext(), NOTIFY_ME_ID,
alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarms.cancel(recurringAlarm);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarms.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, date.getTime(), recurringAlarm);
} else if (Build.VERSION.SDK_INT >= 19) {
alarms.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), recurringAlarm);
} else {
alarms.set(AlarmManager.RTC_WAKEUP, date.getTime(), recurringAlarm);
}
}
This code works correctly in all device on which I have tested. But I am facing issue with Wiko Jerry mt6580.
In this device alarm is triggered after five minutes, of the intended time.
Note: The tests made with the devices connected to charger, to not
active o doze mode.
Any suggestions why I am getting this problem, or how to solve it would be of great help.
-Thanks!

Android Studio Alarm Service Not Starting Automatically

I am trying to develop a simple alarm clock app in Android Studio, but I am having trouble figuring out how to use the AlarmManager and BroadcastReceiver properly.
In my MainActivity I have instantiated everything I need for the AlarmManager (Intent, PendingIntent, and I am using a Calendar to hold the time received from a TimePicker). I have this line in a button's onClick event
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
The receiver is called AlarmReceiver and in its onReceive() I have, for now,
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
Log.e("Receiver", "Receiver entered");
For testing purposes, I set the alarm time to be one minute from the current time whenever I debug the app. After setting the time, I press the button which sets the AlarmManager. The Toast message and Log message don't show up on their own once the time becomes the alarm time. They only show up if I press the button when the time is within the minute that is the alarm time.
For example if alarm time is 3:00, and current time is 2:59 and I have pressed the button to set the AlarmManager. When it becomes 3:00, the messages don't show. They will only show if I press the button within the minute of 3:00.
Is this the normal behavior? If so how do I set it up so that the messages show automatically once everything is set so that I can extend it to a ringtone later?
I have ensured that the time values are right and that the receiver is specified in the manifest
try to something like this:
private static void setUpAlarm(final Context context, final Intent intent, final int timeInterval){
final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
final PendingIntent pi = PendingIntent.getBroadcast(context, timeInterval, intent, 0);
am.cancel(pi);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
final AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(System.currentTimeMillis() + timeInterval, pi);
am.setAlarmClock(alarmClockInfo, pi);
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeInterval, pi);
else
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeInterval, pi);
}
I'm really recommend to use this code:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < android.os.Build.VERSION_CODES.KITKAT){
am.set(AlarmManager.RTC_WAKEUP, nexttime, pi);
} else if (currentapiVersion < android.os.Build.VERSION_CODES.M) {
am.setExact(AlarmManager.RTC_WAKEUP, nexttime, pi);
} else {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nexttime, pi);
}

Android AlarmManager defaulting to 5 minutes when phone screen is off

So I am having an issue with AlarmManager. I am trying to run some code every 2 minutes, which works okay while the phone is awake, but not when the device goes to sleep - during sleep, the intervals are perfectly 5 minutes apart.
Since my desired interval is 2 minutes, this is roughly 250% off my target interval, which for my specific application is not acceptable.
I am aware of the changes in API 19, and have followed suggestions to re-schedule the alarm using setExact() within my BroadcastReceiver. Code is below:
The code used to trigger BroadcastReceiver:
Intent intent = new Intent(this, AlarmReceiver.class);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
mAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 3000, pendingIntent);
And this code is in my BroadcastReceiver which re-schedules the alarm:
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "AlarmService Triggered.", Toast.LENGTH_SHORT).show();
Log.d(this.getClass().getSimpleName(), "Service triggered");
Intent newIntent = new Intent(context, this.getClass());
mPendingIntent = PendingIntent.getBroadcast(context, 0, newIntent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 120000, mPendingIntent);
}
Does anyone have suggestions as to how I could fix this? It is very frustrating that AlarmManager is totally ignoring my wish to have an alarm fire at an exact time. Is there any alternative that will allow me to schedule code at 2 minute intervals like I want?
DEVICE: Samsung Galaxy S6, OS 5.1.1
As #Francesc suggested, it ended up being the Samsung device. Tried it on another phone, now it works flawlessly.
Let this be a lesson to you - don't buy Samsung, they do weird stuff, lol.
Replace ELAPSED_REALTIME_WAKEUP with RTC_WAKEUP.
This is how I schedule alarms:
mTargetTime = System.currentTimeMillis() + timeout * 1000L;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, mTargetTime, mPendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, mTargetTime, mPendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, mTargetTime, mPendingIntent);
}

Android : Alarm Service not working

I am trying to create a simple alarm application where alarm should go off on several days.
I have created a service which registers alarm manager based on data saved in preferences. Everything is working fine except that it does not trigger alarm when app is not running.
AlarmService.java
#Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(this,"Service started",Toast.LENGTH_LONG);
//set separate pending intent values for all days
setIntents();
// register the alarm manager here
alarmManager = (AlarmManager)(getSystemService(Context.ALARM_SERVICE));
execute();
}
private void execute() {
prefs=getSharedPreferences("user_data_pref",Context.MODE_PRIVATE);
prefEditor=prefs.edit();
int hour=prefs.getInt("ALARM_HOUR",0);
int minute=prefs.getInt("ALARM_MINUTE",0);
if(prefs.getString("ALARM_STATUS","NOT_SET").equals("SET"))
isAlarmOn=true;
if(prefs.getString("NOTIFICATION_STATUS","NOT_SET").equals("SET"))
isNotificationOn=true;
if (prefs.getBoolean("MON_SET",false)) {
setAlarmFor(Calendar.MONDAY,hour,minute,mondayIntent,isAlarmOn,isNotificationOn);
}
if (prefs.getBoolean("TUE_SET",false)) {
setAlarmFor(Calendar.TUESDAY,hour,minute,tuesdayIntent,isAlarmOn,isNotificationOn);
}
if (prefs.getBoolean("WED_SET",false)) {
setAlarmFor(Calendar.WEDNESDAY,hour,minute,wednesdayIntent,isAlarmOn,isNotificationOn);
}
if (prefs.getBoolean("THU_SET",false)) {
setAlarmFor(Calendar.THURSDAY,hour,minute,thursdayIntent,isAlarmOn,isNotificationOn);
}
if (prefs.getBoolean("FRI_SET",false)) {
setAlarmFor(Calendar.FRIDAY,hour,minute,fridayIntent,isAlarmOn,isNotificationOn);
}
if (prefs.getBoolean("SAT_SET",false)) {
setAlarmFor(Calendar.SATURDAY,hour,minute,saturdayIntent,isAlarmOn,isNotificationOn);
}
if (prefs.getBoolean("SUN_SET",false)) {
setAlarmFor(Calendar.SUNDAY,hour,minute,sundayIntent,isAlarmOn,isNotificationOn);
}
}
private void setAlarmFor(int weekday, int hour, int minute, PendingIntent pendingIntent, boolean isAlarmOn, boolean isNotificationOn){
if(isAlarmOn){
prefEditor.putString("ALARM_STATUS","SET");
prefEditor.commit();
}
if(isNotificationOn){
prefEditor.putString("NOTIFICATION_STATUS","SET");
prefEditor.commit();
}
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK,weekday);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND,0);
/* Repeating on every week interval */
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
7*24*60*60*1000, pendingIntent);
}
private void setIntents(){
Intent intent = new Intent(this,AlarmReceiver.class);
mondayIntent=PendingIntent.getBroadcast(this, Calendar.MONDAY, intent, 0);
tuesdayIntent=PendingIntent.getBroadcast(this, Calendar.TUESDAY, intent, 0);
wednesdayIntent=PendingIntent.getBroadcast(this, Calendar.WEDNESDAY, intent, 0);
thursdayIntent=PendingIntent.getBroadcast(this, Calendar.THURSDAY, intent, 0);
fridayIntent=PendingIntent.getBroadcast(this, Calendar.FRIDAY, intent, 0);
saturdayIntent=PendingIntent.getBroadcast(this, Calendar.SATURDAY, intent, 0);
sundayIntent=PendingIntent.getBroadcast(this, Calendar.SUNDAY, intent, 0);
}
It should trigger AlarmReceiver class that displays alarm.
AlarmReceiver.java
#Override
public void onReceive(Context context, Intent intent) {
prefs=context.getSharedPreferences("user_data_pref",Context.MODE_PRIVATE);
//Get Status of currently set alarm and notification
alarmStatus=prefs.getString("ALARM_STATUS","NOT_SET");
notificationStatus=prefs.getString("NOTIFICATION_STATUS","NOT_SET");
//Send notification if the notification is set
if(notificationStatus.equals("SET")){
Toast.makeText(context, "NOTIFICATION START SUCCESSFUL", Toast.LENGTH_SHORT).show();
playNotificationSound(context,getNotificationSound());
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentTitle("") // title for notification
.setContentText("") // message for notification
.setAutoCancel(true); // clear notification after click
Intent intent_1 = new Intent(context, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 11, intent_1, Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
displayNotification(context);
}
//Fire Alarm if the Alarm is set
if(alarmStatus.equals("SET")){
Toast.makeText(context, "ALARM START SUCCESSFUL", Toast.LENGTH_SHORT).show();
/** Creating Alarm */
Intent i = new Intent(context,FitterfoxAlarmActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
AndroidManifest.xml
<receiver android:name=".OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".AlarmReceiver"></receiver>
<service android:name=".AlarmService"></service>
Try this set of code your service never going to stop it's works for me. "alarmManager.setRepeating" not working in many of the lollipop and marshmallows device once it started then lose control over the service this will help you out.
if (android.os.Build.VERSION.SDK_INT >= 23) {
piLR = PendingIntent.getBroadcast(context, 0, intentLR,
PendingIntent.FLAG_UPDATE_CURRENT);
amLR.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
interval, piLR);
} else if (android.os.Build.VERSION.SDK_INT >= 19
&& android.os.Build.VERSION.SDK_INT < 23) {
piLR = PendingIntent.getBroadcast(context, 0, intentLR,
PendingIntent.FLAG_UPDATE_CURRENT);
amLR.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), interval, piLR);
} else {
amLR.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), interval, piLR);
}
Starting with KitKat, changes were made to the AlarmManager to improve device battery life. The time at which alarms fire is less exact. If you want your alarms to fire within minutes of the scheduled time, you will need to change the way you are using AlarmManager.
For KitKat (API 19) and later builds, the times specified in setRepeating() are inexact, as described in the documentation. The docs also note:
If your application needs precise delivery times then it must use
one-time exact alarms, rescheduling each time.
You indicate that the alarms do not fire when the app is not running. Because the period for your alarms is one week, there can be a large difference between the scheduled time and the actual time the alarm is fired. The documentation indicates:
Your alarm's first trigger will not be before the requested time, but
it might not occur for almost a full interval after that time.
I suspect that with your current code, you would eventually see the alarm fire, but might have to wait a very long time after the expected time.
Your revised code will probably need to use setWindow().
Marshmallow introduced Doze mode, which further changes alarm scheduling. As #Andy noted in his answer, to get the beavior you want on Marshmallow and later devices, you will need to use setExactAndAllowWhileIdle().
Two other things that need to be fixed. Your PendingIntents should probably use flag PendingIntent.FLAG_UPDATE_CURRENT. In your receiver, the flag Intent.FLAG_ACTIVITY_NEW_TASK belongs on intent_1, not pi.

Categories

Resources