AppWidget在调用updateAppWidget()时无法可靠地更新

时间:2013-10-08 13:15:46

标签: android android-appwidget

我正在Android上创建一个相当常见的AppWidget用例。

  • AppWidgetProvider调用onAppWidgetOptionsChanged()(可伸缩小部件)和onUpdate()(定时)
  • 从这些方法开始IntentService。来自选项的案例已更改我在意图中传递新大小。
  • 该服务与Web服务联系,构建RemoteView并调用updateAppWidget()

我的主要测试设备是Nexus 7(2012)运行库存4.3(库存启动器)

窗口小部件不使用RemoteViewFactory,也不使用AlarmManager。这是一个静态视图,在XML中定义了一个恒定的时间。

它大部分时间都有效,但有时Launcher会完全忽略对updateAppWidget()的调用,并且屏幕上不会进行任何更新。如果我强行关闭启动器,清除缓存并重新调整窗口小部件的大小(强制更新),然后更新。

我认为这与更新频率有关,因为我把IntentService中的一些东西搞砸了,每当它调整大小时,只调用最后一个意图(当用户停止搞乱小部件时)并且它软化了一点问题。

让我们展示一些简化的代码(我相信它是非常标准的):

public class AlbumWidgetService extends WidgetUpdateIntentService {

   @Override
   protected void onHandleIntent(Intent intent) {
       // get's widgetID or array of IDs and pass to 'doTheJob'
   }

   private void doTheJob(int appWidgetId, int heightInDp, int widthInDp) {

      // ...
      // here goes code with pre calculations and get data
      // ...

      // create Intent and PendingIntent with some extras
      Intent intent = ... etc
      PendingIntent pi = PendingIntent.getActivity( ... etc

      // get url for some images    
      List<String> imageFilenames = getImagesFilename(albumId, totalImages);

      // Create the remote view
      RemoteViews views = new RemoteViews(getPackageName(), R.layout.album_widget);

      // ...
      // here goes a bunch of code that load bitmaps from the URLs
      // set text and colors in the remote view
      // put ImageViews into the remote view, etc
      // ...

      try {
         AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
         appWidgetManager.updateAppWidget(appWidgetId, views);
         Log.d(this, "Updating the widget id " + appWidgetId);
      } catch (Exception e) {
         // this exception happens if the RemoteView is too big, have too many bitmaps.
         // I'm already minizing this to happen with the pre calculations, but better safe than sorry
         Log.e(this, "Failed to update the widget id " + appWidgetId, e);
      }
   }

正如我所说,这个东西大部分都有用(我可以看到Log,我可以看到屏幕上的结果。但是每隔一段时间它就会在调整大小后不会更新,即使你能看到Log和它没有崩溃或任何事情。

想法?

0 个答案:

没有答案
相关问题