irobotxx
irobotxx

Reputation: 6063

How to restart and update a widget from a system reboot android

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

Upvotes: 6

Views: 6339

Answers (1)

jboi
jboi

Reputation: 11892

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.

Upvotes: 8

Related Questions