Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
        }
    }   
like image 256
irobotxx Avatar asked Jan 07 '12 10:01

irobotxx


People also ask

How do I restart widgets?

To re-add the same widget, tap and hold on an empty space on your home screen, tap Widgets, and select the one you want.

How do you update a widget?

Full update: Call AppWidgetManager. updateAppWidget(int, android. widget. RemoteViews) to fully update the widget.

Why does my app widget keep stopping?

It turns out that this is a feature of Android where widgets are blocked for apps that are installed to the SD card. This was my problem since I had just moved all of my apps the SD card to save space.


1 Answers

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.

like image 65
jboi Avatar answered Nov 14 '22 22:11

jboi