In the development of Android projects, sometimes in order to achieve better interaction with the user, in the notice bar this small corner, we usually need to enrich the content, this time we need to implement a custom notification bar, such as the following 360 or NetEase style:
The first thing we need to know is the type of control supported by the custom layout file: Notification's custom layout is remoteviews, so it only supports Framelayout, LinearLayout, Relativelayout three layout controls, while supporting AnalogClock, chronometer, Button, ImageButton, ImageView, ProgressBar, TextView, Viewflipper , ListView, GridView, StackView, and Adapterviewflipper these UI controls. For other unsupported controls, the ClassNotFoundException exception will be thrown when used.
At the same time, we also need to understand that notification supports intent types (all instances of the Pendingintent Class).
Here is the specific implementation: In this notification bar we put a progress bar
Get the Notification Manager String ns = Context.notification_service; Notificationmanager nm = (notificationmanager) ctx.getsystemservice (NS); Create Notification objectint icon = R.drawable.robot; Charsequence tickertext = "Hello"; long when = System.currenttimemillis (); Notification Notification = new Notification (icon, Tickertext, when);//set Contentview using Setlatesteveninfo Intent Intent = new Intent (intent.action_view); Intent.setdata (Uri.parse ("http://www.google.com")); pendingintent pi = pendingintent.getactivity (ctx, 0, intent, 0);// Notification.setlatesteventinfo (CTX, "title", " Text ", pi);//Use the default style notification.contentintent = pi; Notification.contentview = new Remoteviews (Ctx.getpackagename (), r.layout.noti); Send notificationnm.notify (1, notification);
The effect of the implementation is as follows: (right is the system default style)
This is just a simple example, in order to achieve our own effect we just need to modify the layout file is OK.