Ogulcan Orhan
Ogulcan Orhan

Reputation: 5317

Issues about Alarm Manager in Android

I'm using a Alarm Manager through with Service for updating widget instance. While doing this, I suppose missing some important spots.

This is how I set the Alarm Manager:

public class MyWidget extends AppWidgetProvider {

    public void onEnabled(Context ctx) {
        alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, MyService.class);
        pi = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

        // Repeat value depends on preferences.
        Long repeat = Long.parseLong(prefs.getString("update_preference", "600"));
        alarmManager.setRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime(), 1000*repeat, pi); 
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, MyService.class);
        PendingIntent pi = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.cancel(pi);
    }

}

Let me explain them:

  1. If device got a call or make a call, Edge/3G not working well. How do I handle a call? If I could, is it a possible to restart/trigger alarm manager after the call? Or any other suggestion appreciated.
  2. When device asleep (a few minutes), after the unlock alarm manager works immediately even if interval value was exceed. But if asleep takes long time, after the unlock alarm manager and also widget receiver not working. What's the way of overstepping asleep for every repeat.
  3. How to control reboot progress? I mean after the reboot service should be start for widget update. Not sure the way of starting.

Any help or suggestions will be great.

Upvotes: 1

Views: 674

Answers (1)

Muzikant
Muzikant

Reputation: 8090

Some of your questions are not very clear so I'll try and answer to the best of my understanding:

  1. You can register a broadcast receiver for TelephonyManager.ACTION_PHONE_STATE_CHANGED
  2. Change your setRepeating call to use AlarmManager.RTC_WAKEUP instead of AlarmManager.RTC. Read more here
  3. You can register a broadcast receiver for Intent.ACTION_BOOT_COMPLETED. This must be registered as a receiver on your app manifest. Read more here.

Upvotes: 4

Related Questions