lightbulb_outline Please take our October 2018 developer survey. Start survey
added in version 1.0.0
belongs to Maven artifact androidx.slice:slice-core:1.0.0

SliceProvider

public abstract class SliceProvider
extends ContentProvider

java.lang.Object
   ↳ 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 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>
 

See also:

Summary

Inherited constants

Public constructors

SliceProvider(String... autoGrantPermissions)

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

SliceProvider()

Public methods

final int bulkInsert(Uri uri, ContentValues[] values)
Bundle call(String method, String arg, Bundle extras)
final Uri canonicalize(Uri url)
final int delete(Uri uri, String selection, String[] selectionArgs)
List<Uri> getPinnedSlices()

Returns a list of slice URIs that are currently pinned.

final String getType(Uri uri)
final Uri insert(Uri uri, ContentValues values)
abstract Slice onBindSlice(Uri sliceUri)

Implemented to create a slice.

final boolean onCreate()
abstract boolean onCreateSliceProvider()

Implement this to initialize your slice provider on startup.

Collection<Uri> onGetSliceDescendants(Uri uri)

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

Uri onMapIntentToUri(Intent intent)

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

void onSlicePinned(Uri sliceUri)

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

void onSliceUnpinned(Uri sliceUri)

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

final Cursor query(Uri uri, String[] projection, Bundle queryArgs, CancellationSignal cancellationSignal)
final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal)
final int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)

Inherited methods

Public constructors

SliceProvider

added in version 1.0.0
public SliceProvider (String... autoGrantPermissions)

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.

SliceProvider

added in version 1.0.0
public SliceProvider ()

Public methods

bulkInsert

added in version 1.0.0
public final int bulkInsert (Uri uri, 
                ContentValues[] values)

Parameters
uri Uri

values ContentValues

Returns
int

call

public Bundle call (String method, 
                String arg, 
                Bundle extras)

Parameters
method String

arg String

extras Bundle

Returns
Bundle

canonicalize

added in version 1.0.0
public final Uri canonicalize (Uri url)

Parameters
url Uri

Returns
Uri

delete

added in version 1.0.0
public final int delete (Uri uri, 
                String selection, 
                String[] selectionArgs)

Parameters
uri Uri

selection String

selectionArgs String

Returns
int

getPinnedSlices

added in version 1.0.0
public List<Uri> getPinnedSlices ()

Returns a list of slice URIs that are currently pinned.

Returns
List<Uri> All pinned slices.

getType

added in version 1.0.0
public final String getType (Uri uri)

Parameters
uri Uri

Returns
String

insert

added in version 1.0.0
public final Uri insert (Uri uri, 
                ContentValues values)

Parameters
uri Uri

values ContentValues

Returns
Uri

onBindSlice

added in version 1.0.0
public abstract Slice onBindSlice (Uri sliceUri)

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.

Parameters
sliceUri Uri

Returns
Slice

onCreate

added in version 1.0.0
public final boolean onCreate ()

Returns
boolean

onCreateSliceProvider

added in version 1.0.0
public abstract boolean onCreateSliceProvider ()

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.

Returns
boolean true if the provider was successfully loaded, false otherwise

onGetSliceDescendants

added in version 1.0.0
public Collection<Uri> onGetSliceDescendants (Uri 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.

Returns
Collection<Uri> All slices within the space.

onMapIntentToUri

added in version 1.0.0
public Uri onMapIntentToUri (Intent intent)

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.

Parameters
intent Intent

Returns
Uri Uri representing the slice associated with the provided intent.

See also:

onSlicePinned

added in version 1.0.0
public void onSlicePinned (Uri sliceUri)

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

added in version 1.0.0
public void onSliceUnpinned (Uri sliceUri)

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.

Parameters
sliceUri Uri

See also:

query

added in version 1.0.0
public final Cursor query (Uri uri, 
                String[] projection, 
                Bundle queryArgs, 
                CancellationSignal cancellationSignal)

Parameters
uri Uri

projection String

queryArgs Bundle

cancellationSignal CancellationSignal

Returns
Cursor

query

added in version 1.0.0
public final Cursor query (Uri uri, 
                String[] projection, 
                String selection, 
                String[] selectionArgs, 
                String sortOrder)

Parameters
uri Uri

projection String

selection String

selectionArgs String

sortOrder String

Returns
Cursor

query

added in version 1.0.0
public final Cursor query (Uri uri, 
                String[] projection, 
                String selection, 
                String[] selectionArgs, 
                String sortOrder, 
                CancellationSignal cancellationSignal)

Parameters
uri Uri

projection String

selection String

selectionArgs String

sortOrder String

cancellationSignal CancellationSignal

Returns
Cursor

update

added in version 1.0.0
public final int update (Uri uri, 
                ContentValues values, 
                String selection, 
                String[] selectionArgs)

Parameters
uri Uri

values ContentValues

selection String

selectionArgs String

Returns
int