Android Widget在重启后无法正常工作

时间:2012-04-14 10:03:59

标签: android

  

可能重复:
  Why widget does not work after reboot?

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

这是BroadcastReceiver:

    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,所以我不知道我是否应该指定不同的意图动作。谢谢。

0 个答案:

没有答案