Android custom notification add click event

preface

In the previous article, "Notification user defined interface", we implemented a user-defined interface, so how can we add click events to the user-defined interface? For example, cool dog has "previous song", "next song" and other control buttons in the notification bar. We need to respond to the click event of the button, but the method is different from the previous click settings and needs to be handled separately. I will give a brief description below.

realization

Similarly, we need a subclass myservice of service, and then set it in oncreate of myservice, as shown in the following code:

As you can see, we first get an object of broadcastreceiver, and then implement our operation in onreceiver. I set the mobile phone to vibrate for one second when clicking. Of course, don't forget to add the permission of vibration in the configuration file, otherwise an error will occur at that time. If you don't know anything about broadcasting, you can first learn about the broadcasting mechanism. Here I use the method of dynamically registering broadcasting. There is another method to register, but I prefer dynamic registration.

Summary

When you see the progress prompt of adding a ProgressBar to the notification to realize the download progress, you need to use the knowledge of updating the notification interface. Although it is different from updating the interface in the activity, it is not complicated, because I don't use this knowledge, so I won't introduce it here. If you are interested, you can search the relevant content.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>