A custom Android notification bar DEMO with buttons, androiddemo

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.