2012-07-30 47 views
1

我被困在一个特定的scenerio中。只要用户从应用更新时间,我就需要更新我的小部件。我曾尝试通过向Intent Extras发送数据尝试广播,但未能这样做。目前,我在AppWidgetProvider中有我的数据,并且我需要将此数据发送到服务从App发送数据到Widget

公共类CountdownWidget扩展AppWidgetProvider {0}共享上下文userDefaults;

// update rate in milliseconds 
public static final int UPDATE_RATE = 1800000; // 30 minute 

public static String nameOne = ""; 

@Override 
public void onDeleted(Context context, int[] appWidgetIds) { 
    for (int appWidgetId : appWidgetIds) { 
     setAlarm(context, appWidgetId, -1); 
    } 
    super.onDeleted(context, appWidgetIds); 
} 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 

    Bundle extras = intent.getExtras(); 
    nameOne = extras.getString("NAME_ONE"); 

    Log.e("Name: ", nameOne); 

    super.onReceive(context, intent); 
} 

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
     int[] appWidgetIds) { 

    for (int appWidgetId : appWidgetIds) { 
     setAlarm(context, appWidgetId, UPDATE_RATE); 
    } 
    super.onUpdate(context, appWidgetManager, appWidgetIds); 
} 

public static void setAlarm(Context context, int appWidgetId, int updateRate) { 

    // Log.e("", "Updating Widget Service"); 

    PendingIntent newPending = makeControlPendingIntent(context, 
      CountdownService.UPDATE, appWidgetId); 
    AlarmManager alarms = (AlarmManager) context 
      .getSystemService(Context.ALARM_SERVICE); 
    if (updateRate >= 0) { 
     alarms.setRepeating(AlarmManager.ELAPSED_REALTIME, 
       SystemClock.elapsedRealtime(), updateRate, newPending); 
    } else { 
     // on a negative updateRate stop the refreshing 
     alarms.cancel(newPending); 
    } 
} 

public static PendingIntent makeControlPendingIntent(Context context, 
     String command, int appWidgetId) { 
    Intent active = new Intent(context, CountdownService.class); 
    active.setAction(command); 
    active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
    // this Uri data is to make the PendingIntent unique, so it wont be 
    // updated by FLAG_UPDATE_CURRENT 
    // so if there are multiple widget instances they wont override each 
    // other 
    Uri data = Uri.withAppendedPath(
      Uri.parse("countdownwidget://widget/id/#" + command 
        + appWidgetId), String.valueOf(appWidgetId)); 
    active.setData(data); 
    return (PendingIntent.getService(context, 0, active, 
      PendingIntent.FLAG_UPDATE_CURRENT)); 
} 

}

正如你看到的名称一是一个静态变量。我可以使用getExtras接收关于onReceive方法的数据,但我无法将此数据传递给我的CountdownService服务。

我确实尝试过CountdownWidget.nameOne但仍然无法提取服务中的数据。

任何帮助将不胜感激。

回答

0

您必须创建更新小部件的服务,请检查this tutorial(第4部分),您可以尝试通过静态变量传输数据。

+0

请查看更新的问题,我尝试使用静态变量,但未能获得数据。 – muneikh 2012-07-30 10:55:05

2

我这样做是通过在构建更新窗口小部件的目的中添加额外功能来实现的。此外,当小部件具有该信息时,它可以再次传递到RemoteViewsService中,用于填充小部件的视图

在我的UpdateWidget帮助器方法中,我传递了userId(由应用程序中的各种活动调用,可能需要通知它需要自行更新的小部件)。

public static void updateWorkoutsWidget(Context context) { 
    Intent intent = new Intent(context, WorkoutsWidget.class); 
    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
    intent.putExtra(WorkoutsWidgetService.USER_ID_EXTRA, userId); 
    int[] ids = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, WorkoutsWidget.class)); 
    if(ids != null && ids.length > 0) { 
     intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids); 
     context.sendBroadcast(intent); 
    } 

然后小部件有一个字段来保存userId并在onReceive中更新它。我将它传递到带有用于创建集合适配器的Intent的RemoteViewsService中。

public class WorkoutsWidget extends AppWidgetProvider { 
    private long userId; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if(intent.getLongExtra(WorkoutsWidgetService.USER_ID_EXTRA, -1) != -1) { 
      userId = intent.getLongExtra(WorkoutsWidgetService.USER_ID_EXTRA, -1); 
     } 
     super.onReceive(context, intent); 
    } 

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     // update each of the app widgets with the remote adapter 
     for (Integer widgetId : appWidgetIds) { 
      Intent intent = new Intent(context, WorkoutsWidgetService.class); 
      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId); 
      intent.putExtra(WorkoutsWidgetService.USER_ID_EXTRA, userId); 
      intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); 

      RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_workouts); 
      rv.setRemoteAdapter(R.id.workouts_list, intent); 

      rv.setEmptyView(R.id.workouts_list, R.id.empty_view); 

      //(Pending intent stuff ommitted for brrevity) 

      appWidgetManager.updateAppWidget(widgetId, rv); 
     } 
     super.onUpdate(context, appWidgetManager, appWidgetIds); 
    } 
} 

然后一切结束了连续传递通过的RemoteViewsService到其RemoteViewsFactory:

public class WorkoutsWidgetService extends RemoteViewsService { 
    public static final String TRACK_WORKOUT_ACTION = "com.bodybuilding.mobile.service.TRACK_WORKOUT_ACTION"; 
    public static final String USER_ID_EXTRA = "userId"; 

    @Override 
    public RemoteViewsFactory onGetViewFactory(Intent intent) { 
     return new WorkoutWidgetViewsFactory(this.getApplicationContext(), intent); 
    } 

    class WorkoutWidgetViewsFactory implements RemoteViewsFactory, ServiceConnection { 

     WorkoutWidgetViewsFactory(Context context, Intent intent) { 
      this.context = context; 
      appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); 
      userId = intent.getLongExtra(USER_ID_EXTRA, -1); 
     } 

     //Lots of other stuff in here 
    } 
}