Skip to Content
Android 9 Development Cookbook - Third Edition
book

Android 9 Development Cookbook - Third Edition

by Rick Boyer
October 2018
Intermediate to advanced
464 pages
15h 17m
English
Packt Publishing
Content preview from Android 9 Development Cookbook - Third Edition

There's more...

You may have noticed the following line of code:

.setAutoCancel(true);

.setAutoCancel() tells the OS to automatically remove the notification when the user clicks on it. This is great if the user presses the notification to turn off the light, but what happens if they use the toggle button? The light will turn off as it should, but they are left with a useless notification. To fix that, we can add a new method to cancel the notification:

private void cancelNotification() {    NotificationManager notificationManager = (NotificationManager)            this.getSystemService(Context.NOTIFICATION_SERVICE);    notificationManager.cancelAll();}

Then we call it when they press the button. Here's how clickLight() will look:

public void clickLight
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Android Application Development Cookbook - Second Edition

Android Application Development Cookbook - Second Edition

Rick Boyer, Kyle Mew
Android High Performance Programming

Android High Performance Programming

Enrique López Mañas, Diego Grancini
Android Things Projects

Android Things Projects

Francesco Azzola

Publisher Resources

ISBN: 9781788991216Supplemental Content