android - 为什么小部件在重启后不起作用?

标签 android widget reboot

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

代码:

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));
    }

最佳答案

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

关于android - 为什么小部件在重启后不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9231495/

相关文章:

android - 如何将 ListView 构建器与 Provider 一起使用?

android - 使用 Jack APIs v01 编译失败 - ConfigurationException : Error while parsing proguard-android. txt

javascript - 如何通过设置时间隐藏或显示小部件

linux - NodeJS 永远无法在启动时工作

java - 持有对 Singleton 的引用的 Activity 会泄漏内存吗?

java - 使用另一个类中的字符串设置 TextView 文本

android - 如何从android中的小部件打开外部应用程序?

javascript - Twitter 意图回调

ios - 设备重启后的本地通知

在 Linux 系统中更改日期,但重启后新日期丢失