开发者

Android alarmmanager not being very reliable

开发者 https://www.devze.com 2023-02-07 01:31 出处:网络
I am having a problem with the Alarmmanager functions for the Android. The problem is alarms that have over an hour or so to wait fail to go off.

I am having a problem with the Alarmmanager functions for the Android.

The problem is alarms that have over an hour or so to wait fail to go off.

My application initially creates an alarm like so:-

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 triggers my RecieverHandler class, specifically 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();

            }


    }

This then launches a service by the name of MessageDispatcher and this function is called:-

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

This function gets the next alarm time from my Database, this I am sure is working correctly, it then sets a new alarm based on the date from the database like so:-

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 the alarm for the next message.

I have tested this over a short amount of time and it appears to work and have tested it over large amounts of time by changing my date and time within the phone. It appears to fire off successfully.

Then when this alarm goes off it gets the next alarm to go off an开发者_如何学JAVAd schedules this. I am nearly 100% sure these parts are working fine.

So I am stuck with only some theories of why it’s not working.

I thought it might be related to me disconnecting the phone from the debugger but the alarm seems to work over short amounts of time in that case.

So my main theory is that the alarmmanager I am creating is being deleted after a certain amount of time? If this is true this a big problem since I need this to be working no matter how much time has passed.

Any help into ensuring my alarm remains is greatly appreciated, thanks.


So my main theory is that the alarmmanager I am creating is being deleted after a certain amount of time?

Alarms that are registered 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 have not indicated:

  • how you are determining whether the alarm is "going off"
  • what the BroadcastReceiver is doing

Without that information, it is impossible to say where you are going wrong.

Make sure that either you are doing all your work in the BroadcastReceiver (if the work is very quick) or that you are maintaining your own WakeLock as you pass control to the IntentService that is doing the rest of the work. Check out WakefulIntentService for more.

Also, you might try to create unique Intents per alarm, rather than updating the current one in place. I am not aware that there is a particular problem here, but it makes me nervous.

0

精彩评论

暂无评论...
验证码 换一张
取 消