Notification customization interface
preface
Before making a mobile phone player, you need to display the control playback interface in the notification bar, as follows:
This can be achieved by making the service run in the foreground (refer to my previous article, service runs in the foreground). Today, we will implement the user-defined interface of notification. Of course, we will not implement the user-defined interface shown in the above figure, but the following simple interface to build the interface we want according to our own needs.
As you can see, I have implemented a simple interface, including an ImageView and a button. Now I'll talk about how to implement it. It's actually very simple.
realization
First, we need to prepare an interface file:
notification.xml
Then create a new subclass of service, myservice:
It can be seen that in the oncreate method, we set the interface no longer by using layoutingflater to get the interface, but by using remoteviews to create an interface. The construction method passes in the package name and the ID of the interface resource, and then we set notification.contentview to our new custom interface.
Summary
Ordinary notification can be used for notification, but when there are special needs, we need to customize the interface, and sometimes we need to add click methods to the customized interface. For example, there is a button in the interface above. How to respond to the click of the button is a difficult problem, Because this is not a simple setonclicklistener, it needs another implementation and broadcasting mechanism. I will explain how to add click events to the custom interface of notification in the next article.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.