0

可能重复:
为什么小部件在重新启动后不起作用?

我有一个通过服务定期更新的 appwidget。现在当系统重新启动时,appwidget 不再显示或响应点击服务。我尝试通过使用广播接收器来启动服务,但到目前为止还没有运气。有什么问题?...

这是广播接收器:

    public void onReceive(final Context context, Intent intent) {
        Log.d(TAG, "received boot completed broadcast receiver... starting service");
        mycontext = context;
        String action = intent.getAction();

        if(action.equals(Intent.ACTION_BOOT_COMPLETED)){

        int[] allids = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, WeatherWidgetProvider.class));
        for(int appWidgetId:allids){

            Intent weatherSettings = new Intent(context, WeatherService.class);
             weatherSettings.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
             mycontext.startService(weatherSettings);

        }
    }   
  }

这就是我从服务中检索 appwidget 的方式:

 public void onStart(Intent intent, int startId){

         context = getApplicationContext();

      if(intent.getExtras()!= null){
      appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID); //get this here, also from the configuration class

   }

      RemoteViews remoteView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.weather_appwidget);
      AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());

      /*get sharedPreferences Value*/
      prefs = context.getSharedPreferences(WeatherForecastConfigure.WEATHER_PREF_NAME, MODE_WORLD_READABLE);

      city = prefs.getString(CITY + appWidgetId,"nothing");
      chk_celsius = prefs.getBoolean(CHECKED_CELSIUS + appWidgetId, true);
      updateRate = WeatherForecastConfigure.convertToMillis(prefs.getInt(REFRESH_UPDATE + appWidgetId, 1));

             //web service call and update the widget here    
     }

我也从配置类中获取了 appwidgetId,所以我不知道是否应该指定不同的意图操作。谢谢你。

4

0 回答 0