私は毎分毎分更新されるウィグデットを持っています(ただし、電話が起きているときのみ)。ただし、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);
}
}
}