Preface
In the previous post"Notification Custom Interface"We have implemented a custom interface, so how should we add click events to the custom interface? For example, Kugou has control buttons such as "Previous Song" and "Next Song" in the notification bar. We need to respond to the button click event, but the method is different from the previous click settings and needs to be handled separately. I will give a brief explanation below.
accomplish
Similarly, we need a subclass of Service MyService, and then set it in MyService's onCreate, as follows:
public class MyService extends Service { public static final String ONCLICK = ""; private BroadcastReceiver receiver_onclick = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (().equals(ONCLICK)) { Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); (1000); } } }; @Override public void onCreate() { (); Notification notification = new Notification(.ic_launcher, "JcMan", ()); RemoteViews view = new RemoteViews(getPackageName(),); = view; IntentFilter filter_click = new IntentFilter(); filter_click.addAction(ONCLICK); //Register broadcast registerReceiver(receiver_onclick, filter_click); Intent Intent_pre = new Intent(ONCLICK); //Get PendingIntent PendingIntent pendIntent_click = (this, 0, Intent_pre, 0); //Set up the monitoring (,pendIntent_click); //Foreground operation startForeground(1, notification); } @Override public IBinder onBind(Intent intent) { return null; } }
As you can see, we first get an object of BroadcastReceiver, and then implement our operations in onReceiver. I set it to make the phone vibrate for a second when clicked. Of course, don't forget to add vibration permissions to the configuration file, otherwise an error will occur at that time. If you don’t know much about broadcasting, you can first understand the broadcasting mechanism. Here I am using the method of dynamically registering broadcasting, and there is another method to register, but I prefer dynamic registration.
summary
I saw the update of the Notification interface to add a ProgressBar to realize the download progress prompt. Here I need to use the knowledge to update the Notification interface. Although it is different from updating the interface in Activity, it is not complicated. Because I did not use this knowledge, I will not introduce it to you here. If you are interested, you can search for related content.