简体   繁体   中英

How to pause onCreate until boot completed?

I host widgets in my app and it seems, that if i start my app before booting of the device is completed, widgets cannot be created properly. Widgets then seem to be not loaded completely or not initialized/updated correctly. For instance: BatteryBotIndicator-Widget, which shows the battery status in percentage, shows a value of "XX" instead of some number like "70%". If i then restart my app and try to recreate the widget with:

AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);

it gives me null for appWidgetInfo (i fetch appWidgetId from SQLite database). And widget cannot be recreated.

It is kind of difficult to debug the real cause in this situation to find out what is exactly causing this (Starting Debugger at the right time). I assume the AppWidgetManager is not ready yet or something.

What i can say for sure: If i wait until i receive the broadcast-event BOOT_COMPLETED all widgets are created properly.

So how can i pause the execution of onCreate until booting is completed?

I can think of putting the thread to sleep in a while loop until the BroadcastReceiver (BOOT_COMPLETED) is setting a bool-variable in application-data to true. But i dont want to wait for this event always at starting of my app, as you can imagine ;)

if i start my app before booting of the device is completed, widgets cannot be created properly.

please explain more what exactly do you mean when you say - "not properly" . this might be relevant to understand if you're widget really depends on something related to the boot.

I can think of putting the thread to sleep in a while loop until the BroadcastReceiver (BOOT_COMPLETED) is setting a bool-variable in application-data to true

very bad idea.. also from performances and design reasons

instead, why not simply register to boot complete broadcast from the manifest, and when you recevice it - simply send the relevant broadcast to update your widget? AppWidgetProvider is already extends BroadcastReceiver, so you can simply add it intent filter for boot complete broadcast.

this is how to add intent filter to boot complete:

 <receiver android:name="MyWidgetProvider" >
        <intent-filter >
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            <action  android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>

        <meta-data
         ...
    </receiver>

and this is how to react to it from the widget provider implementation:

  @Override
  public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
        doTheUpdateHereExactlyLikeHowYouUpdateItFromAnywhereElse();
    } else {
        super.onReceive(context,intent);
    }
}

for more information - follow this tutorial : https://laaptu.wordpress.com/2013/08/12/android-update-app-widget-with-listview-after-phone-reboot/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM