Sys track

Sys track

  • NA
  • 3
  • 2.8k

Alaram manager not working when app is killed

Jan 10 2020 1:58 AM
  1. boolean alarm = (PendingIntent.getBroadcast(this, 0, new Intent("ALARM"), PendingIntent.FLAG_NO_CREATE) == null);  
  2.   
  3. if (alarm) {  
  4.     Intent itAlarm = new Intent("ALARM");  
  5.     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, itAlarm, 0);  
  6.     Calendar calendar = Calendar.getInstance();  
  7.     calendar.setTimeInMillis(System.currentTimeMillis());  
  8.     calendar.add(Calendar.SECOND, 3);  
  9.     AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);  
  10.  int interval = 60000;  
  11.     alarme.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);  
  12. }else {  
  13.     Intent alarmIntent = new Intent(MainActivity.this, BroadcastManager.class);  
  14.     alarmIntent.setAction(Intent.ACTION_MAIN);  
  15.     alarmIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
  16.     PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);  
  17.   
  18.     AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);  
  19.  int interval = 60000;  
  20.   
  21.     manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);  
  22. }  
  23.   
  24. br = new BroadcastManager();  
  25. IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);  
  26. filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);  
  27. this.registerReceiver(br, filter);  

Answers (1)