Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

SliceProvider

abstract class SliceProvider : ContentProvider, CompatWrapped
kotlin.Any
   ↳ android.content.ContentProvider
   ↳ androidx.slice.SliceProvider

A SliceProvider allows an app to provide Slices to the Android OS. A slice is a piece of app content and actions that can be displayed outside of the app in Android system surfaces or within another app. Slices are identified by a Uri and a SliceProvider allows your app to provide a slice based on a uri.

The primary method to implement in SliceProvider is onBindSlice(Uri) which is called whenever something wants to display a slice from your app. An app can have multiple slices all served from the same slice provider, the Uri passed to onBindSlice will identify the specific slice being requested.

public MySliceProvider extends SliceProvider {
 
       public Slice onBindSlice(Uri sliceUri) {
           String path = sliceUri.getPath();
           switch (path) {
               case "/weather":
                   return createWeatherSlice(sliceUri);
               case "/traffic":
                   return createTrafficSlice(sliceUri);
           }
           return null;
       }
  }
  

Slices are constructed with androidx.slice.builders.TemplateSliceBuilders.

Slices are not currently live content. They are bound once and shown to the user. If the content in the slice changes due to user interaction or an update in the data being displayed, then ContentResolver#notifyChange(Uri, ContentObserver) should be used to notify the system to request the latest slice from the app.

The provider needs to be declared in the manifest to provide the authority for the app. The authority for most slices is expected to match the package of the application.

<provider
      android:name="com.android.mypkg.MySliceProvider"
      android:authorities="com.android.mypkg" />
  

Slices can also be identified by an intent. To link an intent with a slice, the slice provider must have an IntentFilter matching the slice intent. When a slice is being requested via an intent, onMapIntentToUri(Intent) will be called and is expected to return an appropriate Uri representing the slice.

<provider
      android:name="com.android.mypkg.MySliceProvider"
      android:authorities="com.android.mypkg">
      <intent-filter>
          <action android:name="android.intent.action.MY_SLICE_INTENT" />
          <category android:name="android.app.slice.category.SLICE" />
      </intent-filter>
  </provider>
  

Summary

Public constructors
<init>(@NonNull vararg autoGrantPermissions: String!)

A version of constructing a SliceProvider that allows autogranting slice permissions to apps that hold specific platform permissions.

Public methods
Int
bulkInsert(@NonNull uri: Uri, @NonNull values: Array<ContentValues!>)

open Bundle!
call(method: String!, arg: String!, extras: Bundle!)

Uri?
canonicalize(@NonNull url: Uri)

Int
delete(@NonNull uri: Uri, @Nullable selection: String?, @Nullable selectionArgs: Array<String!>?)

open MutableList<Uri!>

Returns a list of slice URIs that are currently pinned.

String!
getType(uri: Uri!)

Uri?
insert(@NonNull uri: Uri, @Nullable values: ContentValues?)

abstract Slice!
onBindSlice(sliceUri: Uri!)

Implemented to create a slice.

Boolean

abstract Boolean

Implement this to initialize your slice provider on startup.

open MutableCollection<Uri!>!

Obtains a list of slices that are descendants of the specified Uri.

open Uri

This method must be overridden if an IntentFilter is specified on the SliceProvider.

open Unit
onSlicePinned(sliceUri: Uri!)

Called to inform an app that a slice has been pinned.

open Unit
onSliceUnpinned(sliceUri: Uri!)

Called to inform an app that a slices is no longer pinned.

Cursor?
query(@NonNull uri: Uri, @Nullable projection: Array<String!>?, @Nullable selection: String?, @Nullable selectionArgs: Array<String!>?, @Nullable sortOrder: String?)

Cursor?
query(@NonNull uri: Uri, @Nullable projection: Array<String!>?, @Nullable queryArgs: Bundle?, @Nullable cancellationSignal: CancellationSignal?)

Cursor?
query(@NonNull uri: Uri, @Nullable projection: Array<String!>?, @Nullable selection: String?, @Nullable selectionArgs: Array<String!>?, @Nullable sortOrder: String?, @Nullable cancellationSignal: CancellationSignal?)

Int
update(@NonNull uri: Uri, @Nullable values: ContentValues?, @Nullable selection: String?, @Nullable selectionArgs: Array<String!>?)

Public constructors

<init>

SliceProvider(@NonNull vararg autoGrantPermissions: String!)

A version of constructing a SliceProvider that allows autogranting slice permissions to apps that hold specific platform permissions.

When an app tries to bind a slice from a provider that it does not have access to, the provider will check if the caller holds permissions to any of the autoGrantPermissions specified, if they do they will be granted persisted uri access to all slices of this provider.

Parameters
autoGrantPermissions String!: List of permissions that holders are auto-granted access to slices.

<init>

SliceProvider()

Public methods

bulkInsert

fun bulkInsert(@NonNull uri: Uri, @NonNull values: Array<ContentValues!>): Int

call

open fun call(method: String!, arg: String!, extras: Bundle!): Bundle!

canonicalize

@Nullable fun canonicalize(@NonNull url: Uri): Uri?

delete

fun delete(@NonNull uri: Uri, @Nullable selection: String?, @Nullable selectionArgs: Array<String!>?): Int

getPinnedSlices

@NonNull open fun getPinnedSlices(): MutableList<Uri!>

Returns a list of slice URIs that are currently pinned.

Return
MutableList<Uri!>: All pinned slices.

getType

fun getType(uri: Uri!): String!

insert

@Nullable fun insert(@NonNull uri: Uri, @Nullable values: ContentValues?): Uri?

onBindSlice

abstract fun onBindSlice(sliceUri: Uri!): Slice!

Implemented to create a slice.

onBindSlice should return as quickly as possible so that the UI tied to this slice can be responsive. No network or other IO will be allowed during onBindSlice. Any loading that needs to be done should happen in the background with a call to ContentResolver#notifyChange(Uri, ContentObserver) when the app is ready to provide the complete data in onBindSlice.

onCreate

fun onCreate(): Boolean

onCreateSliceProvider

abstract fun onCreateSliceProvider(): Boolean

Implement this to initialize your slice provider on startup. This method is called for all registered slice providers on the application main thread at application launch time. It must not perform lengthy operations, or application startup will be delayed.

You should defer nontrivial initialization (such as opening, upgrading, and scanning databases) until the slice provider is used (via #onBindSlice, etc). Deferred initialization keeps application startup fast, avoids unnecessary work if the provider turns out not to be needed, and stops database errors (such as a full disk) from halting application launch.

Return
Boolean: true if the provider was successfully loaded, false otherwise

onGetSliceDescendants

open fun onGetSliceDescendants(uri: Uri!): MutableCollection<Uri!>!

Obtains a list of slices that are descendants of the specified Uri.

Implementing this is optional for a SliceProvider, but does provide a good discovery mechanism for finding slice Uris.

Parameters
uri Uri!: The uri to look for descendants under.
Return
MutableCollection<Uri!>!: All slices within the space.

onMapIntentToUri

@NonNull open fun onMapIntentToUri(intent: Intent!): Uri

This method must be overridden if an IntentFilter is specified on the SliceProvider. In that case, this method can be called and is expected to return a non-null Uri representing a slice. Otherwise this will throw UnsupportedOperationException.

Return
Uri: Uri representing the slice associated with the provided intent.

onSlicePinned

open fun onSlicePinned(sliceUri: Uri!): Unit

Called to inform an app that a slice has been pinned.

Pinning is a way that slice hosts use to notify apps of which slices they care about updates for. When a slice is pinned the content is expected to be relatively fresh and kept up to date.

Being pinned does not provide any escalated privileges for the slice provider. So apps should do things such as turn on syncing or schedule a job in response to a onSlicePinned.

Pinned state is not persisted through a reboot, and apps can expect a new call to onSlicePinned for any slices that should remain pinned after a reboot occurs.

Parameters
sliceUri Uri!: The uri of the slice being unpinned.

onSliceUnpinned

open fun onSliceUnpinned(sliceUri: Uri!): Unit

Called to inform an app that a slices is no longer pinned.

This means that no other apps on the device care about updates to this slice anymore and therefore it is not important to be updated. Any syncs or jobs related to this slice should be cancelled.

query

@Nullable fun query(@NonNull uri: Uri, @Nullable projection: Array<String!>?, @Nullable selection: String?, @Nullable selectionArgs: Array<String!>?, @Nullable sortOrder: String?): Cursor?

query

@Nullable fun query(@NonNull uri: Uri, @Nullable projection: Array<String!>?, @Nullable queryArgs: Bundle?, @Nullable cancellationSignal: CancellationSignal?): Cursor?

query

@Nullable fun query(@NonNull uri: Uri, @Nullable projection: Array<String!>?, @Nullable selection: String?, @Nullable selectionArgs: Array<String!>?, @Nullable sortOrder: String?, @Nullable cancellationSignal: CancellationSignal?): Cursor?

update

fun update(@NonNull uri: Uri, @Nullable values: ContentValues?, @Nullable selection: String?, @Nullable selectionArgs: Array<String!>?): Int