Android status bar notification status bar Notification

Source: Internet
Author: User

Three ways to notify users of Android:

1.Toast Notification

2.Dialog Notification

3.Status bar Notification status bar Notification notification

Sending a status bar notification must use two classes: notificationmanager,notification

1.NotificationManager is a system service that must be obtained via Getsystemservice ()

Notificationmanager notificationmanager= (Notificationmanager) Getsystemservice (Notification_service);

2.Notification is the specific status bar notification object

Call Notificationmanager's Notify () method to create the notification

Two parts:

①: status bar notification

Notification.icon=r.drawable.ic_launcher;

notification.tickertext= "My first notification";

Notification.when=system.currenttimemillis ();

②: drop-down notification list and click to jump:

Two different ways:

One: Setlatesteventinfo () method

Context context = Getapplicationcontext ();

Charsequence contenttitle= "Notification";

Charsequence contenttext= "Notification Context";

Intent intent=new Intent (main.this,turn.class);

Pendingintent pendingintent=pendingintent.getactivity (main.this, 0, intent, 0);

Notification.setlatesteventinfo (context, Contenttitle, ContentText, pendingintent);

Two: Custom notification bar

Notification.flags=notification.flag_auto_cancel; Notify auto-Cancel after user clicks

Set two variables Contentview and contentintent

Remoteviews contenview=new remoteviews (Getpackagename (), r.layout.notification_layout);

Contenview.setimageviewresource (R.id.icon, R.drawable.ic_launcher);

Contenview.settextviewtext (R.id.contenttext, "Custom Notification");

Notification.contentview=contenview;

Intent intent1=new Intent (main.this,turn.class);

Pendingintent pendingintent1=pendingintent.getactivity (main.this, 0, intent1, 0);

Notification.contentintent=pendingintent1;

Tips:

Errors you may encounter: couldn ' t expand remoteviews for:

Check if it is a remoteviews corresponding layout that uses a component that it does not support

Check if the layout file for the remoteviews has a basic error, such as forgetting to declare the width higher

Android status bar notification status bar Notification

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.