Launch the Slice Viewer tool via ADB (command line)
Run your app from Android Studio:
adb install -t -r <yourapp>.apk
View your Slice by running the following command:
adb shell am start -a android.intent.action.VIEW -d slice-<your slice URI>
Slice Viewer showing a single WiFi Slice
View all of your Slices in one place
In addition to launching a single Slice, you can view a persistent list of your Slices.
Use the search bar to manually search for your Slices via URI (for example, content://com.example.android.app/hello). Each time you search, the Slice is added to the list.
Any time you launch the Slice Viewer tool with a Slice URI, the Slice is added to the list.
You can swipe a Slice to remove it from the list.
Tap the URI of the Slice to see a page containing only that Slice. This has the same effect as launching Slice Viewer with a Slice URI.
Slice Viewer showing a list of Slices
View the Slice in different modes
An app that presents a Slice can modify the SliceView#mode at runtime, so you should make sure your Slice looks as expected in each mode. Select the menu icon in the top-right area of the page to change the mode.
Single Slice viewer with mode set to "small"
Build your first Slice
To build a Slice, open your Android Studio project, right-click your src package, and select New... > Other > Slice Provider. This creates a class that extends SliceProvider, adds the required provider entry to your AndroidManifest.xml, and modifies your build.gradle to add the required Slice dependencies.
The modification to AndroidManifest.xml is shown below:
Each Slice has an associated URI. When a surface wants to display a Slice, it sends a binding request to your app with this URI. Your app then handles this request and dynamically builds the Slice via the onBindSlice method. The surface can then display the Slice when appropriate.
Below is an example of an onBindSlice method that checks for the /hello URI path and returns a Hello World Slice:
Use the slice run configuration that you created in the Slice Viewer section above, passing in your Slice URI (for example, slice-content://com.android.example.slicesample/hello) of the Hello World Slice to view it in the Slice Viewer.
Interactive Slices
Similar to notifications, you can handle clicks within your Slice by attaching PendingIntent objects that are triggered on user interaction. The example below starts an Activity that can receive and handle those intents:
In this example, while the count is shown, it doesn’t update on its own. You can modify your broadcast receiver to notify the system that a change has occurred by using ContentResolver#notifyChange.