A custom Android notification bar DEMO with buttons, androiddemo
We know that the Notification class and icationicationmanager class can be used for Android development to conveniently build system Notification bar messages. Below we will briefly describe how to implement a custom Notification bar with buttons.
Create RemoteViews, R. layout. notification is the layout file of the custom notification bar;
RemoteViews remoteViews = new RemoteViews (getPackageName (), R. layout. notification );
RemoteViews. setTextViewText (R. id. TV _up, "Capital Airport boutique wireless ");
RemoteViews. setTextViewText (R. id. TV _down, "free access ");
Click Event processing in the Custom button. Common examples are shortcut keys (playing/pausing, last or next) in the notification bar of various music players;
Intent intent = new Intent (ACTION_BTN );
Intent. putExtra (INTENT_NAME, INTENT_BTN_LOGIN );
PendingIntent intentpi = PendingIntent. getBroadcast (this, 1, intent, PendingIntent. FLAG_UPDATE_CURRENT );
RemoteViews. setOnClickPendingIntent (R. id. btn_login, intentpi );
Generally, you can click to enter the program page in the notification bar as follows:
Intent intent2 = new Intent ();
Intent2.setClass (this, MainActivity. class );
Intent2.setFlags (Intent. FLAG_ACTIVITY_NEW_TASK | Intent. FLAG_ACTIVITY_CLEAR_TASK );
PendingIntent intentContent = PendingIntent. getActivity (this, 0, intent2, PendingIntent. FLAG_UPDATE_CURRENT );
Build icationicationcompat. Builder and SET related attributes of the notification bar;
NotificationCompat. Builder builder = new NotificationCompat. Builder (this );
Builder. setOngoing (false );
Builder. setAutoCancel (false );
Builder. setContent (remoteViews );
Builder. setTicker ("using Capital Airport Wireless ");
Builder. setSmallIcon (R. drawable. id_airport); // note that if this attribute is not set, the notification bar will not be displayed on some models.
Notification notification = builder. build ();
Notification. defaults = Notification. DEFAULT_SOUND;
Notification. flags = Notification. FLAG_NO_CLEAR;
Notification. contentIntent = intentContent;
Build icationicationmanager and display the notification bar;
Icationicationmanager NotificationManager = (notificationManager) getSystemService (Context. icationication_service );
Icationicationmanager. Y (0, notification );
Now, a simple notification bar with button customization is almost complete, and then you can register a BroadcastReceiver to respond to button events.
Source code: source code