开发者

setRepeating not working for widget

I've got a wigdet that is updated every minutes on the minute (but only when the phone is awake). However, after calling setRepeating() in the onEnabled method, the alarm doesn't ever seem to go off. What am I doing wrong? Here's (what I think) is the relevant code.

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_u开发者_如何学JAVApdate_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);
    }
}
}


Problem was in my Manifest file. Apparently I can't use a string resources as the name of a action to filter. In other words, this is not this doesn't work:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜