我有一个每分钟更新一次的 wigdet(但仅在手机唤醒时)。但是,在 onEnabled 方法中调用 setRepeating() 之后,警报似乎永远不会响起。我究竟做错了什么?这是(我认为)是相关代码。
public class BusStopWidgetProvider extends AppWidgetProvider{
private static AlarmManager alarmManager;
private static PendingIntent pendingIntent;
private static LinkSchedule linkSchedule;
@Override
public void onReceive(Context context, Intent intent){
super.onReceive(context, intent);
Log.i("special", "in on recieve with" + intent.getAction());
if(
intent.getAction().equals(context.getString(R.string.widget_update_action)) ||
intent.getAction().equals(Intent.ACTION_TIME_CHANGED) ||
intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED))
{
AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
ComponentName appWidgetName =
new ComponentName(context, BusStopWidgetProvider.class);
int[] appWidgetIds = widgetManager.getAppWidgetIds(appWidgetName);
onUpdate(context, widgetManager, appWidgetIds);
}
}
@Override
public void onEnabled(Context context){
linkSchedule = LinkSchedule.getLinkSchedule(context.getResources());
Intent updateIntent =
new Intent(context.getString(R.string.widget_update_action));
pendingIntent =
PendingIntent.getBroadcast(context, 0, updateIntent, 0);
alarmManager =
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
GregorianCalendar nextMinute = (GregorianCalendar)Calendar.getInstance();
nextMinute.add(Calendar.MINUTE, 1);
nextMinute.set(Calendar.SECOND, 0);
alarmManager.setRepeating(
AlarmManager.RTC,
nextMinute.getTimeInMillis(),
60000,
pendingIntent);
}
@Override
public void onDisabled(Context context){
alarmManager.cancel(pendingIntent);
}
@Override
public void onUpdate(
Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
final int N = appWidgetIds.length;
for(int i=0; i<N; i++){
int appWidgetId = appWidgetIds[i];
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.bus_stop_widget);
views.setTextViewText(R.id.time, linkSchedule.getNextTime(context.getString(R.string.sexton_name)));
views.setTextViewText(R.id.stopLabel, context.getString(R.string.sexton_name));
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}