Receiver Class
public class MyAlarmReceiver extends BroadcastReceiver { private AlarmManager alarmManager; private PowerManager powerManager; private PowerManager.WakeLock wakeLock; @Override public void onReceive(Context context, Intent intent) { try { powerManager = (PowerManager) context.getSystemService(POWER_SERVICE); wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TimeTickWakeLog"); wakeLock.acquire(5 * 1000); MyLog.d("alarm manager : begin onReceive"); } catch (Exception ex) { ex.printStackTrace(); MyLog.ex(ex); if (wakeLock != null) { wakeLock.release(); powerManager = null; wakeLock = null; } } try { int isServiceRunning = 0; ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (MyMqttService.class.getName().equals(service.service.getClassName())) { // service is running isServiceRunning++; } } if (isServiceRunning == 0) { // service is not running, start it Intent serviceIntent = new Intent(context, MyMqttService.class); context.startService(serviceIntent); } // connect to mqtt broker if needed if (Statics.isAppInBackground()) { EventBus.getDefault().post(new ReconnectMqttEvent()); } } catch (Exception ex) { ex.printStackTrace(); MyLog.ex(ex); } // the reason for separating try/catches is to insure next alarm manager will be scheduled try { // region reschedule alarm manager long timeToWait = 60 * 1000; alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent myIntent = new Intent(context, MyAlarmReceiver.class); PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + timeToWait, alarmIntent); } else { alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + timeToWait, alarmIntent); } // endregion MyLog.d("alarm manager : end onReceive"); if (wakeLock != null) { wakeLock.release(); powerManager = null; wakeLock = null; } } catch (Exception ex) { ex.printStackTrace(); MyLog.ex(ex); if (wakeLock != null) { wakeLock.release(); powerManager = null; wakeLock = null; } } } }
Manifest file
<receiver android:name=".MyAlarmReceiver"> </receiver>
onCreate of Service
// region schedule alarm manager for the first time long timeToWait = 60 * 1000; alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, MyAlarmReceiver.class); PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + timeToWait, alarmIntent); } else { alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + timeToWait, alarmIntent); } // endregion
Rereferences
https://developer.android.com/training/scheduling/alarms
https://stackoverflow.com/questions/4459058/alarm-manager-example
https://www.javatpoint.com/android-alarmmanager