Added in API level 36

AppFunctionService


abstract class AppFunctionService : Service
kotlin.Any
   ↳ android.content.Context
   ↳ android.content.ContextWrapper
   ↳ android.app.Service
   ↳ android.app.appfunctions.AppFunctionService

Extend this class to implement app functions, that can be executed by AppFunctionManager.executeAppFunction.

AppFunctionManager.executeAppFunction targeting an app function provided by this service will bind to this service (waking your app if necessary). A single AppFunctionService implementation can implement multiple app functions, by examining the ExecuteAppFunctionRequest.getFunctionIdentifier in the onExecuteFunction method.

You must declare the service and its associated app functions in your AndroidManifest.xml. The <service> manifest entry must:

  • Require the BIND_APP_FUNCTION_SERVICE permission to ensure that only the system can bind to the service.
  • Declare an <intent-filter> with the SERVICE_INTERFACE action to indicate to the system that it provides app functions.
  • Reference an XML asset that defines the metadata for the functions provided by this service using a <property> named android.app.appfunctions. See AppFunctionMetadata for details on the XML schema.

Example manifest declaration:

<code>&lt;service
      android:name=".YourAppFunctionService"
      android:permission="android.permission.BIND_APP_FUNCTION_SERVICE"&gt;
    &lt;property
        android:name="android.app.appfunctions.v2"
        android:value="your_app_functions.xml" /&gt;
    &lt;intent-filter&gt;
      &lt;action android:name="android.app.appfunctions.AppFunctionService" /&gt;
    &lt;/intent-filter&gt;
  &lt;/service&gt;
  </code>

Important: Only one AppFunctionService implementation can be active in an app at a time.

Summary

Constants
static String

The Intent filter that must be declared as handled by the service.

Inherited constants
Public constructors

Public methods
IBinder
onBind(intent: Intent?)

Return the communication channel to the service.

abstract Unit
onExecuteFunction(request: ExecuteAppFunctionRequest, callingPackage: String, callingPackageSigningInfo: SigningInfo, cancellationSignal: CancellationSignal, callback: OutcomeReceiver<ExecuteAppFunctionResponse!, AppFunctionException!>)

Called by the system following an AppFunctionManager.executeAppFunction call that targets an app function identifier referenced by the XML asset associated with this service.

Inherited functions

Constants

SERVICE_INTERFACE

Added in API level 36
static val SERVICE_INTERFACE: String

The Intent filter that must be declared as handled by the service.

To be supported, the service must also require the BIND_APP_FUNCTION_SERVICE permission so that other applications can not abuse it.

Value: "android.app.appfunctions.AppFunctionService"

Public constructors

AppFunctionService

AppFunctionService()

Public methods

onBind

Added in API level 36
fun onBind(intent: Intent?): IBinder

Return the communication channel to the service. May return null if clients can not bind to the service. The returned android.os.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 may be null.
Return
IBinder This value cannot be null.

onExecuteFunction

Added in API level 36
abstract fun onExecuteFunction(
    request: ExecuteAppFunctionRequest,
    callingPackage: String,
    callingPackageSigningInfo: SigningInfo,
    cancellationSignal: CancellationSignal,
    callback: OutcomeReceiver<ExecuteAppFunctionResponse!, AppFunctionException!>
): Unit

Called by the system following an AppFunctionManager.executeAppFunction call that targets an app function identifier referenced by the XML asset associated with this service.

You can determine which function to execute using ExecuteAppFunctionRequest.getFunctionIdentifier(). This allows your service to route the incoming request to the appropriate logic for handling the specific function. See ExecuteAppFunctionRequest for how to retrieve the function's arguments.

This method is always triggered in the main thread. You should run heavy tasks on a worker thread and dispatch the result with the given callback. You should always report back the result using the callback, no matter if the execution was successful or not.

The implementation should try to respect the provided CancellationSignal, if possible.
This method must be called from the main thread of your app.

Parameters
request ExecuteAppFunctionRequest: The function execution request.
This value cannot be null.
callingPackage String: The package name of the app that is requesting the execution. It is strongly recommended that you do not alter your function’s behavior based on this value. Your function should behave consistently for all callers to ensure a predictable experience. Starting in android.os.Build.VERSION_CODES#CINNAMON_BUN, the value will always be an empty string.
This value cannot be null.
callingPackageSigningInfo SigningInfo: The signing information of the app that is requesting the execution. It is strongly recommended that you do not alter your function’s behavior based on this value. Your function should behave consistently for all callers to ensure a predictable experience. Starting in android.os.Build.VERSION_CODES#CINNAMON_BUN, the value will always be an unknown signing info.
This value cannot be null.
cancellationSignal CancellationSignal: A signal to cancel the execution.
This value cannot be null.
callback OutcomeReceiver<ExecuteAppFunctionResponse!, AppFunctionException!>: A callback to report back the result or error.
This value cannot be null.