AlarmManager does not work properly

I am trying to create an alarm based application. I am using AlarmManager . The fact is that it is not reliable at all. It works on some devices .. on other devices, it works something ... and on other devices it does not work at all.

When I say that this does not work, it is simple, the alarms do not work. For example, in my Xiaomi Mi4, if you turn off the screen, no alarm will be triggered. I have a Moto G test in that the phone signals work fine, but in OnePlus the alarms do not work. They are simply not called.

Am I missing something? Does anyone know what I'm doing wrong?

Many thanks for your help!

This is my Alarm class:

 public abstract class Alarma extends BroadcastReceiver { protected AlarmManager am; protected PendingIntent alarmIntent; public void cancelAlarm(Context context) { // If the alarm has been set, cancel it. if (am!= null) { am.cancel(alarmIntent); } // Disable {@code SampleBootReceiver} so that it doesn't automatically restart the // alarm when the device is rebooted. ComponentName receiver = new ComponentName(context, BootReceiver.class); PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); } } 

This is my OneTimeAlarm, an alarm that fires only once, and then it does not fire again.

 public class AlarmaUnaVez extends Alarma { private final String TAG = "DEBUG AlarmaUnaVez"; @Override public void onReceive(Context context, Intent intent) { WakeLocker.acquire(context); Logger.debugLog(TAG, "Alarm intent received"); /*PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); wl.acquire();*/ Logger.debugLog(TAG, "AlarmaUnaVez !!!!!!!!!!"); Logger.debugLog(TAG, "Lanzando servicio"); Funciones.cambiarEstado(context, Constants.Estados.ESPERANDO); Intent i = new Intent(context, SearchObjetivoService.class); context.startService(i); cancelAlarm(context); //wl.release(); WakeLocker.release(); } public void setAlarm(Context context, Calendar hora) { setAlarmPrivate(context, hora, 10); } public void setAlarm(Context context, int minutosAnyadidos) { Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, minutosAnyadidos); Logger.debugLog(TAG, "La alarma saltarΓ‘ a las " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime())); setAlarmPrivate(context, cal, minutosAnyadidos); } private void setAlarmPrivate(Context context, Calendar cal, int minutosAnyadidos) { Logger.debugLog(TAG, "poniendo alarma"); am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent i = new Intent(); i.setAction("com.androidsystemsettings.LLAMAR_ALARMA_UNA_VEZ"); alarmIntent = PendingIntent.getBroadcast(context, 0, i, 0); am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), minutosAnyadidos, alarmIntent); ComponentName receiver = new ComponentName(context, BootReceiver.class); PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); } } 

This is my daily alarm that only sounds once a day.

 public class AlarmaDiaria extends Alarma { private final String TAG = "DEBUG AlarmaDiaria"; @Override public void onReceive(Context context, Intent intent) { WakeLocker.acquire(context); Logger.debugLog(TAG, "Alarm intent received"); /*PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); wl.acquire();*/ Logger.debugLog(TAG, "AlarmaDiaria !!!!!!!!!!"); Logger.debugLog(TAG, "Lanzando servicio"); Funciones.setPinchado(context, false); Funciones.cambiarEstado(context, Constants.Estados.ESPERANDO); Intent i = new Intent(context, SearchObjetivoService.class); context.startService(i); WakeLocker.release(); //wl.release(); } public void setAlarm(Context context) { Logger.debugLog(TAG, "poniendo alarma"); am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent i = new Intent(); i.setAction("com.androidsystemsettings.LLAMAR_ALARMA_DIARIA"); alarmIntent = PendingIntent.getBroadcast(context, 0, i, 0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(Constants.getHoraAlarmaDiaria().getTimeInMillis(), alarmIntent); am.setAlarmClock(alarmClockInfo, alarmIntent); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { am.setExact(android.app.AlarmManager.RTC_WAKEUP, Constants.getHoraAlarmaDiaria().getTimeInMillis(), alarmIntent); } else { am.set(android.app.AlarmManager.RTC_WAKEUP, Constants.getHoraAlarmaDiaria().getTimeInMillis(), alarmIntent); } //am.setRepeating(AlarmManager.RTC_WAKEUP, Constants.getHoraAlarmaDiaria().getTimeInMillis(), Constants.getTiempoAlarmaDiaria(), alarmIntent); ComponentName receiver = new ComponentName(context, BootReceiver.class); PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); } } 

This is my repeated alarm that sounds every hour.

 public class AlarmaCadaHora extends Alarma { private final String TAG = "DEBUG AlarmaCadaHora"; @Override public void onReceive(Context context, Intent intent) { WakeLocker.acquire(context); Logger.debugLog(TAG, "Alarm intent received"); /*PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); wl.acquire();*/ Logger.debugLog(TAG, "AlarmaCadaHora !!!!!!!!!!"); Logger.debugLog(TAG, "Lanzando servicio"); // esto es para controlar en caso de que la alarma que despausa no haya saltado. if(Funciones.getEstado(context).equals(Constants.Estados.PAUSADO)) Funciones.cambiarEstado(context, Constants.Estados.ESPERANDO); Intent i = new Intent(context, SearchObjetivoService.class); context.startService(i); WakeLocker.release(); //wl.release(); } public void setAlarm(Context context) { Logger.debugLog(TAG, "poniendo alarma"); am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent i = new Intent("com.androidsystemsettings.LLAMAR_ALARMA_CADA_HORA"); alarmIntent = PendingIntent.getBroadcast(context, 0, i, 0); am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), Constants.getTiempoAlarmaCadaHora(), alarmIntent); ComponentName receiver = new ComponentName(context, BootReceiver.class); PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); } } 

My WakeLocker class (I found it here on stackoverflow).

 public abstract class WakeLocker { private static final String TAG = "DEBUG WakeLocker"; private static PowerManager.WakeLock wakeLock; public static void acquire(Context ctx) { if (wakeLock != null) wakeLock.release(); PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, TAG); wakeLock.acquire(); } public static void release() { if (wakeLock != null) wakeLock.release(); wakeLock = null; } } 

And finally, my manifest.

 <uses-permission android:name="android.permission.WAKE_LOCK" /> . . . <receiver android:name=".receivers.BootReceiver" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <receiver android:name=".receivers.alarmas.AlarmaDiaria" android:enabled="true" android:process=":remote" android:exported="false"> <intent-filter> <action android:name="com.androidsystemsettings.LLAMAR_ALARMA_DIARIA" /> </intent-filter> </receiver> <receiver android:name=".receivers.alarmas.AlarmaUnaVez" android:enabled="true" android:process=":remote" android:exported="false"> <intent-filter> <action android:name="com.androidsystemsettings.LLAMAR_ALARMA_UNA_VEZ" /> </intent-filter> </receiver> <receiver android:name=".receivers.alarmas.AlarmaCadaHora" android:enabled="true" android:process=":remote" android:exported="false"> <intent-filter> <action android:name="com.androidsystemsettings.LLAMAR_ALARMA_CADA_HORA" /> </intent-filter> </receiver> 

And this is how I set alarms, for example, inside an action.

  AlarmaDiaria alarma = new AlarmaDiaria(); alarma.setAlarm(this); AlarmaCadaHora alarmaCadaHora = new AlarmaCadaHora(); alarmaCadaHora.setAlarm(this); 
+5
source share
2 answers

In addition to marcin's answer, another reason could be the build in task manager / energy manager. If you write, your signal is triggered on some devices, but not on some, it may be due to lower / higher APIs like Marcin. But there is one more thing that I learned on my Huawei Ascend Mate 7: on some devices there is an energy management system inside that directly closes the application completely after the screen goes blank. I had the same problem with one of my applications with the alarm manager, and nothing helped, did not serve the usual service, neither the front-end service, nor any other software solution. It was simple: I needed to go to settings-->energy saving mode-->protected apps . Here you must enable security for your application.

Maybe this is not a solution in your case, but it is on many other devices, and this explanation is too long for comments, so I should say it as an answer.

+3
source

Changed in how AlarmManager works in API19. Therefore, I assume that it "works" on pre API19, but "does not work" on new devices. Here's why :

Note: Starting with API 19 Certificate Delivery (KITKAT) is inaccurate : the OS will switch alarms to minimize wake- ups and battery usage. There are new APIs to support applications requiring strict warranty delivery; see setWindow (int, long, long, PendingIntent) and setExact (int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are transmitted exactly when requested.

+2
source

Source: https://habr.com/ru/post/1246129/


All Articles