如何从系统重新启动 android 来重新启动和更新小部件

发布于 2024-12-25 07:29:02 字数 1509 浏览 1 评论 0原文

我有一个运行成功的应用程序小部件。现在当手机重新启动时, 该小部件会丢失所有数据并仅位于主屏幕上。由于手机重新启动时我无法从广播接收器更新小部件,因此我创建了一个通知 这会导致小部件的配置活动。用户再次设置配置并离开配置活动后,没有任何作用;空闲的小部件就保留在那里?(用户必须删除小部件并再次创建小部件)..假设我没有正确接收小部件ID,或者我在广播接收器中做错了,并假设将所有代码移动到onEnable 在小部件方法中?...我如何正确刷新小部件。请记住,所有小部件更新都是通过服务完成的。

顺便说一下,我在广播接收器中有这段代码用于 boot_completed 操作:

public void onReceive(final Context context, Intent intent) {

        String  info = context.getResources().getString(R.string.restart_setting);

     int[] allids = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, MyWidgetProvider.class));
        for(int appWidgetId:allids){

        NotificationManager mnotifyManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notify = new Notification(R.drawable.icon, "Weather Update", System.currentTimeMillis());
        notify.defaults = Notification.DEFAULT_SOUND;
        notify.defaults = Notification.DEFAULT_VIBRATE;

    Intent Settings = new Intent(context, WidgetConfigure.class);
    Settings.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

        PendingIntent pending = PendingIntent.getActivity(context, 0, weatherSettings, PendingIntent.FLAG_ONE_SHOT);
        notify.setLatestEventInfo(context, "Weather Update", info, pending);
        notify.flags = Notification.FLAG_AUTO_CANCEL;
        notify.defaults = Notification.DEFAULT_ALL;
        mnotifyManager.notify(WEATHER_NOTIFICATION_ID , notify);
        }
    }   

i have an appwidget that runs successfully. now when the phone is being rebooted,
the widget loses all its data and just sits on the home screen. since i can't update the widget from a broadcast receiver when the phone reboots, i created a notification
that leads to the configuration activity of the widget. Nothing works after the user sets the configuration again and leaves the configuration activity; the idle widget just remains there?(the user has to delete the widget and create a widget again).. Am assuming i am not receiving the widget id correctly or am i doing it wrongly in broadcast receiver and suppose to move all the code to onEnable in the widget method?.. How do i refresh the widget correctly. Please bear in mind that all widget updates are done from a service.

by the way i have this code in broadcast receiver for boot_completed action:

public void onReceive(final Context context, Intent intent) {

        String  info = context.getResources().getString(R.string.restart_setting);

     int[] allids = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, MyWidgetProvider.class));
        for(int appWidgetId:allids){

        NotificationManager mnotifyManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notify = new Notification(R.drawable.icon, "Weather Update", System.currentTimeMillis());
        notify.defaults = Notification.DEFAULT_SOUND;
        notify.defaults = Notification.DEFAULT_VIBRATE;

    Intent Settings = new Intent(context, WidgetConfigure.class);
    Settings.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

        PendingIntent pending = PendingIntent.getActivity(context, 0, weatherSettings, PendingIntent.FLAG_ONE_SHOT);
        notify.setLatestEventInfo(context, "Weather Update", info, pending);
        notify.flags = Notification.FLAG_AUTO_CANCEL;
        notify.defaults = Notification.DEFAULT_ALL;
        mnotifyManager.notify(WEATHER_NOTIFICATION_ID , notify);
        }
    }   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍笑 2025-01-01 07:29:02

我今天早上遇到了同样的问题,并尝试通过监听 boot_completion 意图来解决它。它不适用于小部件。这是我发现的。

  • 要获得有关重启的通知,除了 boot_completed 和 android.permission.RECEIVE_BOOT_COMPLETED 的权限之外,您还需要接收 android.intent.action.ACTION_EXTERNAL_APPLICATION_AVAILABLE。< /p>

  • 但事实是,额外的意图是不必要的。重新启动后,BroadcastReceiver 的 onEnableonUpadate 无论如何都会被调用。

因此,我实现的解决方案将每个小部件的配置存储在一个文件中,并将小部件 ID 作为文件名的一部分。在接收器的 onUpdate 中,我再次初始化小部件(使用点击侦听器等),

结果是,重新启动后需要一段时间,但小部件(所有)看起来不错并且按预期工作。

I just had the same problem this morning and tried to solve it with listening to the boot_completion intent. It doesn't work this way with widgets. Here's what I found.

  • To get notified about a reboot, you need to receive android.intent.action.ACTION_EXTERNAL_APPLICATION_AVAILABLE in addition to boot_completed and the permission of android.permission.RECEIVE_BOOT_COMPLETED.

  • But the whole truth is that an additional intent is not necessary. After reboot the onEnable and onUpadate of the BroadcastReceiver get called anyway.

So, the solution I've implemented, stores the config of each widget in a file, with the widget Id as part of the filename. And in onUpdate of the receiver I initialize the widget again (with click listener and all that)

Outcome is, that after reboot it takes a moment but then the widgets (all) look good and work like expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文