October 2018
Intermediate to advanced
464 pages
15h 17m
English
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