Blog Infos
Author
Published
Topics
Author
Published
Android app tight integration with the assistant
What are Slices?

Slices examples

Implementation
Declaring the Slice in Shortcuts.xml
<capability android:name="actions.intent.OPEN_APP_FEATURE">
    <slice>
        <url-template
            android:name="content://com.example.android.app.provider/slice{?feature}" />

    </slice>
</capability>
Creating the SliceProvider
  • onBindSlice(), it takes a Uri, and what you need to do is map this Uri with your slice and return the Slice
  • onCreateSliceProvider(), you can do preliminary tasks, but something light, do not block the thread, and return true.
  • last but not least, you can show the Slice Authority, this is a unique name that will allow the system to trigger the Slice, see Grant Permission below.
class MySliceProvider : SliceProvider() {
    companion object {
        const val SLICE_AUTHORITY = "com.example.android.app.provider"
    }
    override fun onBindSlice(sliceUri: Uri?): Slice? {
        // you can filter the sliceUri here to provide the appropriate slice
        return createSlice()
    }
    override fun onCreateSliceProvider(): Boolean = true
}

 

Job Offers

Job Offers


    Senior Android Developer

    SumUp
    Berlin
    • Full Time
    apply now

    Senior Android Engineer

    Carly Solutions GmbH
    Munich
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

No results found.

Jobs

Declare SliceProvider in AndroidManifest
<application>
...
<provider android:name="MySliceProvider"
    android:authorities="com.example.android.app.provider"
    android:exported="true" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.app.slice.category.SLICE" />
    </intent-filter>
</provider>
Grant Slice permissions
private fun grantAssistantPermissions() {
    getAssistantPackage()?.let { assistantPackage ->
        val sliceProviderUri = Uri.Builder()
            .scheme(ContentResolver.SCHEME_CONTENT)
            .authority(MySliceProvider.SLICE_AUTHORITY)
            .build()

        SliceManager.getInstance(this).grantSlicePermission(assistantPackage, sliceProviderUri)
    }
}
Creating a Slice
// SliceProvider
fun createSlice() {
    return list(...) {
        header {
            title = "My title"
            subtitle = "Subtitle"
            // Defines the primary action when slice is clicked
            primaryAction = SliceAction.create(...)
        }
        row { ... }
    }
}
Previewing with SliceViewer

Releases · android/user-interface-samples

adb install -r -t slice-viewer.apk

You can now execute the following command to start SliceViewer. Notice how the keyword slice has been prefixed to content.

adb shell am start -a android.intent.action.VIEW -d 
slice-content://com.example.android.app.provider/slice

example of slice viewer

Conclusion
Resources

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
This is the second article in an article series that will discuss the dependency…
READ MORE
blog
Let’s suppose that for some reason we are interested in doing some tests with…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu