Reputation: 5317
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:
Any help or suggestions will be great.
Upvotes: 1
Views: 674
Reputation: 8090
Some of your questions are not very clear so I'll try and answer to the best of my understanding:
setRepeating
call to use AlarmManager.RTC_WAKEUP
instead of AlarmManager.RTC
. Read more hereIntent.ACTION_BOOT_COMPLETED
. This must be registered as a receiver on your app manifest. Read more here.Upvotes: 4