Tutorials Android Quick Settings Tile API Example in Android N

Quick Settings Tile API Example in Android N

Quick settings are undoubtedly one of the most popular features in Android devices. It provides a convenient way to quickly change the device settings or key actions directly from the notification panel.

Like always, Google never disappoints developers. This time, Android N introduced the new Quick Settings Tile API and allow developers to add their own quick setting actions. Now Quick Settings Tile adds more room for additional custom actions and you can swipe left/right to access more settings.

Please note, Quick Settings tiles are reserved for controls or actions that are either urgently required or frequently used, and should not be used as shortcuts to launching an app.

Android Quick Settings Tiles can only have icons of a single color. It can be tinted to either white or grey. You can handle click, double-click action on the tiles but the long press is restricted only to few system apps.

Android Quick Settings API Example

In this tutorial, we will see how to use the Quick Settings Tile API in Android N to register a custom tile action. The sample application will add a custom action to quick settings and when clicked it will launch an activity.

You need the following perquisites to run this code sample.

  • Android Studio version 2.0+
  • A test device or emulator configuration with Android version 6.0+

Adding a tile to quick settings involves two steps. First you need to declareQuick Settings Tile in AndroidManifest.xml file. Add the following code snippets inside <application> element.

<service
    android:name="com.stacktips.example.MyAppTileService"
    android:icon="@drawable/tile_icon"
    android:label="@string/tile_label"
    android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
    <intent-filter>
        <action android:name="android.service.quicksettings.action.QS_TILE" />
    </intent-filter>
</service>

Let us now extend the TileService class to respond the Tile events and handle click action to launch an activity when user clicking on the Quick Settings Tile. You can optionally override the following methods to perform different actions when the Tile state changes.

onTileAdded() Called when user adds the tile to the quick settings from the edit interface
onTileRemoved() Called when the tile is removed from the quick settings using the edit interface.
onStartListening() Called when the tile is brought into the listening state.
onStopListening() Called when the tile is brought out of the listening state.
onClick() Called when the tile is clicked.

MyAppTileService.java

public class MyAppTileService extends TileService {
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onTileAdded() {
        super.onTileAdded();
    }

    @Override
    public void onTileRemoved() {
        super.onTileRemoved();
    }

    @Override
    public void onStartListening() {
        super.onStartListening();
    }

    @Override
    public void onStopListening() {
        super.onStopListening();
    }

    @Override
    public void onClick() {
        super.onClick();
        //Start main activity
        startActivity(new Intent(this, MainActivity.class));
    }
}
[download url=”https://github.com/StackTipsLab/AndroidN-Quick-Settings-Tile-Example”]
Total
0
Shares
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Pankaj
Pankaj
6 years ago

Insightful example for bignners. However it is missing the other important aspects of the tile API such as listining to tile state, update tile dynamically.

Adding that to this tut will make it precious

Nilanchala Panigrahy
Nilanchala Panigrahy
6 years ago
Reply to  Pankaj

Thanks Pankaj

Yaswant
Yaswant
3 years ago

Hi. You have mentioned we can handle double-click, could you please explain how to do that. Thanks

Previous Post
Contribute to StackTips

Required Experienced Technical Content Writers for Stacktips.com

Next Post

Getting Started with iCloud Storage – iOS Tutorial

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
3
0
Would love your thoughts, please comment.x
()
x