【问题标题】:Why widget does not work after reboot?为什么小部件在重新启动后不起作用?
【发布时间】:2012-02-10 16:50:54
【问题描述】:

我有这个简单的小部件,当我点击它时,它应该会打开我的活动,它可以工作,但重启后它就不能工作了。我必须删除然后再次将小部件添加到我的主屏幕,因为当我单击小部件时,小部件没有响应并且没有打开我的活动。那么问题出在哪里?

代码:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        for(int i=0; i<N; i++){
            int appWidgetId = appWidgetIds[i];

            context.startService(new Intent(context, WidgetService.class));

            Intent intent = new Intent(context, WidgetDialog.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            views.setOnClickPendingIntent(R.id.layout_widget, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    @Override
    public void onEnabled(Context context){
        context.startService(new Intent(context, WidgetService.class));
    }

    @Override
    public void onDisabled(Context context){
        context.stopService(new Intent(context, WidgetService.class));
    }

【问题讨论】:

  • 找到解决方案了吗?
  • 是的,我做到了。无论如何,谢谢你的提问。
  • 你应该发布你的解决方案,而不是说“谢谢你的提问”。
  • @Adam 你能解释一下你是如何解决这个问题的吗?就像奇怪的答案所暗示的一样?
  • 我在一些旧的 Android 版本上遇到了这个问题,我记得有时在重启后不会调用 OnUpdate 和 OnEnabled 方法,但我认为它现在应该可以正常工作了。

标签: android widget reboot


【解决方案1】:

重新启动后,您将调用 OnEnabled 而不是 onUpdate,因此将代码从 onUpdate 移到单独的方法中,然后调用该方法以从 onEnabled 更新所有小部件

【讨论】:

    【解决方案2】:

    小部件继承的 AppWidgetProvider 在启动时从操作系统接收到ACTION_APPWIDGET_UPDATE 时调用onUpdate

    如果您在不调用super.onReceive() 的情况下覆盖onReceive,则需要自行拦截此Intent。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多