Android alarmmanager is not very reliable

I am having a problem with Alarmmanager features for Android.

The problem is that alarms that have more than an hour or so to wait do not go away.

My application initially creates this signal: -

PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, mCal.getTimeInMillis(), sender); 

When the alarm goes off, it calls my RecieverHandler class, in particular this function: -

 public void onReceive(Context context, Intent intent) { try { Bundle bundle = intent.getExtras(); Intent newIntent = new Intent(context, MessageDispatcher.class); newIntent.putExtras(bundle); // newIntent.addFlags(Intent.FLAG); context.startService(newIntent); } catch (Exception e) { Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } 

Then a service called MessageDispatcher is launched, and this function is called: -

 public int onStartCommand(Intent intent, int flags, int startId) 

This function receives the next alarm time from my database, I am sure it works correctly, and then sets a new alarm based on the date from the database as follows: -

 PendingIntent sender = PendingIntent.getBroadcast(this, 192837, newIntent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, mCal.getSendAt().getTimeInMillis(), sender); 

This creates an alarm for the next message.

I tested it in a short amount of time and it seems to have worked and tested it for a lot of time, changing the date and time on the phone. It seems to have worked successfully.

Then, when this signal goes off, it receives the next alarm and turns it off. I am almost 100% sure that these parts work fine.

So, I am stuck with only some theories why it does not work.

I thought that this could be due to disconnecting the phone from the debugger, but in this case the alarm works for a short period of time.

So, my main theory is that the creator of the alarm clock that I create, is deleted after a certain amount of time? If true, this is a big problem since I need it to work no matter how much time has passed.

Any help in ensuring my anxiety remains very appreciated, thanks.

+4
source share
1 answer

So, my main theory is that the creator of the alarm clock that I create is deleted after a certain amount of time?

Registered registered signals remain registered until you cancel them, or until the next reboot, or until the user kills your application with a “task killer” on Android 2.1 and earlier.

You did not specify:

  • as you define, the alarm is disabled.
  • what does BroadcastReceiver do

Without this information it is impossible to tell where you are mistaken.

Make sure you do all your work in BroadcastReceiver (if the work is very fast) or that you save your own WakeLock when you pass an IntentService control that does the rest of the work. See WakefulIntentService more details.

In addition, you can try to create a unique Intents for each alarm, instead of updating the current one. I don’t know that there is a particular problem here, but it makes me nervous.

+2
source

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


All Articles