# Print output for @column tags ?> CompanionDeviceService - Android SDK | Android Developers

Most visited

Recently visited

CompanionDeviceService

public abstract class CompanionDeviceService
extends Service

java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.app.Service
         ↳ android.companion.CompanionDeviceService


Service to be implemented by apps that manage a companion device. System will keep this service bound whenever an associated device is nearby, ensuring app stays alive. An app must be associated with at leas one device, before it can take advantage of this service. You must declare this service in your manifest with an intent-filter action of SERVICE_INTERFACE and permission of Manifest.permission.BIND_COMPANION_DEVICE_SERVICE

Summary

Constants

String SERVICE_INTERFACE

An intent action for a service to be bound whenever this app's companion device(s) are nearby.

Inherited constants

Public constructors

CompanionDeviceService()

Public methods

final IBinder onBind(Intent intent)

Return the communication channel to the service.

abstract void onDeviceAppeared(String address)

Called by system whenever a device associated with this app is available.

abstract void onDeviceDisappeared(String address)

Called by system whenever a device associated with this app stops being available.

Inherited methods

Constants

SERVICE_INTERFACE

public static final String SERVICE_INTERFACE

An intent action for a service to be bound whenever this app's companion device(s) are nearby.

The app will be kept alive for as long as the device is nearby. If the app is not running at the time device gets connected, the app will be woken up.

Shortly after the device goes out of range, the service will be unbound, and the app will be eligible for cleanup, unless any other user-visible components are running.

An app shouldn't declare more than one of these services. If running in background is not essential for the devices that this app can manage, app should avoid declaring this service.

The service must also require permission Manifest.permission.BIND_COMPANION_DEVICE_SERVICE

Constant Value: "android.companion.CompanionDeviceService"

Public constructors

CompanionDeviceService

public CompanionDeviceService ()

Public methods

onBind

public final IBinder onBind (Intent intent)

Return the communication channel to the service. May return null if clients can not bind to the service. The returned IBinder is usually for a complex interface that has been described using aidl.

Note that unlike other application components, calls on to the IBinder interface returned here may not happen on the main thread of the process. More information about the main thread can be found in Processes and Threads.

Parameters
intent Intent: This value cannot be null.

Returns
IBinder This value may be null.

onDeviceAppeared

public abstract void onDeviceAppeared (String address)

Called by system whenever a device associated with this app is available.
This method must be called from the main thread of your app.

Parameters
address String: the MAC address of the device This value cannot be null.

onDeviceDisappeared

public abstract void onDeviceDisappeared (String address)

Called by system whenever a device associated with this app stops being available. Usually this means the device goes out of range or is turned off.
This method must be called from the main thread of your app.

Parameters
address String: the MAC address of the device This value cannot be null.