2012-07-30 44 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 
    } 
}