setRepeating不适用于widget

时间:2010-12-23 19:47:04

标签: android widget alarmmanager

我有一个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);
    }
}
}

1 个答案:

答案 0 :(得分:0)

问题出现在我的清单文件中。显然我不能使用字符串资源作为要过滤的操作的名称。换句话说,这不是不行的:

<intent-filter>
  <action android:name="@string/name_of_intent"/>
</intent-filter>