Android Studio Add an action button to the status bar

Viewed 158

There are a lot of buttons in the status bar (Wifi, Bluetooth, etc), how can I add a button there which will be connected to a function in my code so when the user press the button the function will get called? (Even if the app isn't open) Image of the buttons that I'm talking about

1 Answers

You can use TileService to add a custom Quick Setting tile of your app.

Add this code in the Manifest file : (make sure you put your label and icon in the code)

<service
    android:name=".MyQSTileService"
    android:label="@string/my_default_tile_label"
    android:icon="@drawable/my_default_icon_label"
    android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
    <intent-filter>
         <action android:name="android.service.quicksettings.action.QS_TILE" />
    </intent-filter>
</service>

And create a MyQSTileService.java class which extends TileService: You can override various methods of the TileService class to perform actions at various events. Here I just overrode the onClick method which will be called when your qs tile is clicked.

public class MyTileService extends TileService {

    @Override
    public void onClick() {
        // Perform your action here
        super.onClick();
    }
}
Related