문제

I want my widget to display its last data (image and text) immediately after rebooting the phone. After reboot, my widget's AppWidgetProvider.onUpdate() is called and I successfully update it there. However, it can take quite a long time before before onUpdate() actually gets called. After reboot, the homescreen first comes up and my widget sits showing its default layout for up to 30 seconds. Finally onUpdate() is called and the widget updates itself. I also tried listening to the BOOT_COMPLETED intent but it takes just as long before the widget receives it.

How does the AccuWeather widget do it? AccuWeather immediately displays its last content as soon as the homescreen appears. It doesn't have a long initial delay before restoring. Is there a way to dynamically save data into a layout so that it is picked up immediately on startup?

Edit: I determined that the AccuWeather widget I mentioned is actually something included by Samsung on their phones. It probably has hooks into the OS to load up early. When I try widgets from the Play Store, all of them seem have a delay after boot before they update their content. It looks like something I will have to live with, but I would appreciated it if anybody has ideas.

도움이 되었습니까?

해결책 2

I posted my question in 2013 when 4.3 Jelly Bean was still the prevalent system and 4.4 KitKat was just starting to be rolled out. I found that KitKat significantly decreased boot time, enough that the delay until widgets are updated is acceptable.

다른 팁

You have to create a boot complete receiver in your Manifest.xml like this:

 <receiver
        android:name=".BootUpReceiver"
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="com.example.boot" />
        </intent-filter>
    </receiver>

Then in BootUpReceiver.java you can to get all your app widget ids and update them:

AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
    ComponentName widgetComponent = new ComponentName(context,MyWidgetProvider.class);
    int[] appWidgetIds = widgetManager.getAppWidgetIds(widgetComponent);

    for(int i=0;i<appWidgetIds.length;i++) {

        //Updating code here
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top