Skip to content

Most visited

Recently visited

navigation

Device Updates

The device update APIs allow an application to control and monitor applying over the air (OTA) software updates to the device.

Updating a device involves performing the following operations:

The default initial state for the device update service is STATE_IDLE. The update service checks in with the update server periodically (every few hours) to determine if a new OTA update was posted through the Android Things Console. When a new update is detected, the UpdateManager reports STATE_UPDATE_AVAILABLE.

Once an update is available, the automatic behavior of the update service depends on the update policy configured for the device. When the policy indicates that an update should be applied, the update service begins downloading and verifying the update contents. To complete the update process, the device must reboot. Unless handled automatically by the update policy, the UpdateManager reports STATE_UPDATED_NEEDS_REBOOT to indicate that your app should reboot the device using the DeviceManager.

Configuring the update policy

The UpdateManager class defines a set of policies that are used to determine which of these steps are taken automatically and which need to be initiated by the device's application software.

When setting the policy, you must also provide a deadline for applying the update. The timeout starts when the device first sees that an update is available. After this timeout, the UpdateManager automatically downloads the update (if needed), applies it, and reboots the device. This provides a safety net so that developers can be certain that updates will eventually be applied, even if the device is using a policy that restricts automatically applying an update, rebooting the device, or both.

The following example could be used by devices that can automatically download and apply an available update, but may not be able to reboot immediately:

UpdateManager manager = new UpdateManager();
manager.setPolicy(UpdateManager.POLICY_APPLY_ONLY, 7, TimeUnit.DAYS);

Monitoring update status

You can add a StatusListener to monitor status changes to UpdateManager, such as when a new update is available or when it has been downloaded to the device. To get the current state, use the UpdateManager.getStatus() method.

When using POLICY_APPLY_ONLY, your app is in control of when to reboot the device to apply the update. For example, you may want to delay until a time when no one will be interacting with the device.

public class UpdateService extends Service {

    UpdateManager mUpdateManager;

    @Override
    public void onCreate() {
        super.onCreate();

        mUpdateManager = new UpdateManager();
        mUpdateManager.setPolicy(UpdateManager.POLICY_APPLY_ONLY, 7, TimeUnit.DAYS);
        mUpdateManager.addStatusListener(mUpdateStatusListener);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        mUpdateManager.removeStatusListener(mUpdateStatusListener);
    }

    private StatusListener mUpdateStatusListener = new StatusListener() {
        @Override
        public void onStatusUpdate(UpdateManagerStatus status) {
            switch (status.currentState) {
                case UpdateManagerStatus.STATE_DOWNLOADING_UPDATE:
                    /* Download in progress */
                    break;
                case UpdateManagerStatus.STATE_FINALIZING_UPDATE:
                    /* Download complete */
                    break;
                case UpdateManagerStatus.STATE_UPDATED_NEEDS_REBOOT:
                    /* Reboot device to apply update */
                    break;
            }
        }
    };

    ...
}

Checking for updates

To perform a manual update check, use the performUpdateNow() method. The policy passed to this method temporarily overrides the policy set by setPolicy(). For example, you can use this during initial device setup to check for an update and then prompt the user to download it.

public class SetupActivity extends Activity {

    private UpdateManager mUpdateManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mUpdateManager = new UpdateManager();
        mUpdateManager.addStatusListener(new StatusListener() {
            @Override
            public void onStatusUpdate(UpdateManagerStatus status) {
                switch (status.currentState) {
                    case UpdateManagerStatus.STATE_UPDATE_AVAILABLE:
                        /* Notify user of the update */
                        break;
                    case UpdateManagerStatus.STATE_DOWNLOADING_UPDATE:
                        /* Update UI to show progress */
                        break;
                }
            }
        });

        // Trigger an update check immediately
        mUpdateManager.performUpdateNow(UpdateManager.POLICY_CHECKS_ONLY);
    }

    ...
}
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)