Android: How to create an alarm in an AppWidgetProvider
my widget should refresh its textviews every day at 0:00. In the widget_provider.xm开发者_运维技巧l I set android:updatePeriodMillis="1000"
but I read that the minimum update period is 30 minutes and I have to use alarmManager for this. So i want an alarm that triggers the refresh every day at 0:00. UpdateService.class handles the refreshing (setting texts for textviews based on date. The class is just not called until around half an hour after midnight)
In the public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
method I am using this code:
Intent intentN = new Intent(context, UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.add(Calendar.HOUR_OF_DAY, 0);
cal.add(Calendar.MINUTE, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent);
In the third line of the copied cut Eclipse says:
The method getSystemService(String) is undefined for the type HelloWidget
HelloWidget is the name of the AppWidgetProvider.
Thanks
Did you try
AlarmManager alarmManager = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);
精彩评论