主页进程重启后解耦的AppWidget意图

时间:2010-02-08 10:50:54

标签: android android-appwidget homescreen

我的Android应用程序Transdroid提供了几个主屏幕小部件。每个AppWidget都有2个“按钮”(ImageButton),一个启动应用程序,一个启动一些刷新AppWidget内容的活动。很简单。 Here是截图。小部件代码位于我的Google代码网站,但最重要的是:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_15);
views.setOnClickPendingIntent(R.id.widget_action, PendingIntent.getActivity(context, 0, new Intent(context, Transdroid.class), 0));
appWidgetManager.updateAppWidget(id, views);

问题是:在重新启动Home进程后,未调用widget的onUpdate,因此用于将功能附加到按钮的PendingIntents将丢失。

重现起来相当容易。

  1. 启动模拟器
  2. 添加一个小部件(使用PendingIntent,例如,启动一个活动)
  3. 点击按钮,看它确实有用
  4. 强制杀死主进程('adb -e shell kill 96',其中96是android.process.acore的PID)
  5. 小部件的按钮不再起作用。
  6. 更精确:没有onReceive,因此在重启android.process.acore Home进程时不会调用onUpdate。反过来,没有附加意图。

    任何人都遇到同样的问题并且知道如何规避这个问题?

1 个答案:

答案 0 :(得分:0)

我刚测试了几个自己的appwidgets,他们的点击事件在杀死acore后工作正常。这是我的相关代码:

    final RemoteViews views = new RemoteViews(context.getPackageName(), layoutId);
    views.setOnClickPendingIntent(R.id.widget_view, PendingIntent.getActivity(
            context, 
            0, 
            new Intent(context, MyActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 
            PendingIntent.FLAG_UPDATE_CURRENT));

我能看到的主要区别是意图相关的标志;我会说他们绝对值得一试。 [我的代码中的layoutId参数设置为else;这个相同的代码用于具有不同布局的几个小部件]。