IAN LAKE: When you have to do something really important
right now, unlocking your phone, opening an app,
and navigating to the right place is a lot of work.
On API 24 and higher devices, though, there's
an alternative-- a Quick Settings tile.
Now, your app can provide its own tiles for quick access
to critical functionality.
Once users have manually added your tile,
they'll be able to view your tile by swiping down
the notification tray, either as just an icon if you're
in the first few spots, or with up to two lines of text
and the icon in the full Quick Settings panel.
Even better, the full Quick Settings panel
is available even if the device is locked.
Users can then tap on your Quick Settings tile
to trigger a callback, where you can kick off some background
work, show a dialog, or start an activity.
Now, that's pretty useful for those really frequent
or urgent tasks.
And that's really what Quick Settings tiles are all about.
They're not just for launching your app.
You have a home screen icon for that.
So think about the critical operations that
can be very easily understood with a simple icon
and a quick tap or through a simple dialog
to provide a bit more context.
Now, your Quick Settings tile is going
to be controlled and managed through a tile service.
Note that your app name will be displayed
right next to the name of the Quick Settings tile.
So give a descriptive name and icon that
hints at what it actually does.
Space is pretty limited here, so keep it
under 18 characters for the label.
The icon here should be solid white
on a transparent background.
Think of it as just the alpha channel.
Remember, you can use a vector drawable
as you'll only be enabled on API 24 and higher devices.
The lifecycle of a Quick Settings tile
is a bit different from many services, though.
And there's three main parts.
First, when your tile is added to the Quick Settings panel,
the system will bind to your service and call onTileAdded().
After that, each time your tile is visible,
the system will yet again bind to your tile service and call
onStartListening().
This is the point where you're expected to keep the tile up
to date all the way until you receive a callback
onStopListening(), which is when the tile's no longer visible.
These make a natural pairing if you need to set up
any listeners for changes.
Finally, when-- I mean, if the user were to ever remove
your tile, you'd again have the system bind to your service
and call onTileRemoved().
Keep in mind that the lifecycle is controlled by the system
here.
So don't assume that your service will
persist between these states.
There's another mode called active mode.
Read the blog post linked in the description
for how that differs from the passive mode described here.
OK, let's say we've received a callback to onStartListening()
and we want to update our tile.
You'll call getQsTile to get the tile instance associated
with the tile service.
This is what allows you to update the icon, label,
and content description.
It also allows you to set the state of the tile.
There are three states for a tile.
Active corresponds with the on or enabled state.
Inactive is the off or disabled state.
And the system automatically tints your icon
to indicate the inactive state.
Really useful if you have a tile that naturally fits
with an on-off toggle model.
And finally, the unavailable state--
this would be appropriate if your tile needs internet access
and you're offline, for instance.
Users can't click on your tile when it's unavailable,
unlike the other two states.
Just remember, once you've updated everything you need,
you need to call updateTile.
This is what actually triggers the system to update your tile.
One other pretty core part to a Quick Settings tile,
though-- handling clicks.
You'll get a callback to your tile service's onClick method
whenever the user taps your tile.
From here, you have a few options.
The most common case is you do want to do some background
work-- toggling some internal state in your app
and updating your tile with the new state.
You can also display a dialog using the showDialog method.
This is useful if the tile switches
between more than two states or you
feel the user needs some additional context,
such as what remote device is being changed
or that you need to download or upload some information.
There's also a startActivity and Collapse method
for starting an activity directly from the tile service.
Don't forget, though-- users will
be able to access your tile even if the device is securely
locked.
If you have something sensitive, consider checking isSecure()
and modifying your behavior to only show non-sensitive
information.
If the Quick Settings panel is above the lock screen,
isLocked() will return true.
In this state, you won't be able to use showDialog().
And you'll have to make sure any activity you
start uses FLAG_SHOW_WHEN_LOCKED to show above the lock screen.
There is a convenience method, unlockAndRun(),
that will prompt the user to unlock their device
if your click handling requires an unlocked device
So Quick Settings tiles are a great way
to give quick access to critical functionality in your app.
Make sure they're intuitive and save the user time
throughout the entire lifetime of the app.
And you'll find them a great way to build better apps.