Android Status Bar Notification, Android notification

Source: Internet
Author: User

Android Status Bar Notification, Android notification

Android notifies users in three ways:

1. Toast Notification

2. Dialog Notification

3. Status Bar Notification, Status Bar Notification

Two types of status bar notifications must be sent: icationicationmanager and Notification.

1. icationicationmanager is a system Service and must be obtained through getSystemService ().

Icationicationmanager NotificationManager = (notificationManager) getSystemService (NOTIFICATION_SERVICE );

2. Notification is the specific status bar Notification object.

Call icationicationmanager's notify () method to create 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 methods:

I. 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 );

Ii. Custom notification bar

Notification. flags = Notification. FLAG_AUTO_CANCEL; the notification is automatically canceled when the user clicks

Set two variables: contentView and contentIntent.

RemoteViews contenView = new RemoteViews (getPackageName (), R. layout. icationication_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:

Possible error: Couldn't expand RemoteViews:

Check whether the components not supported by RemoteViews are used in layout.

Check whether the layout file corresponding to RemoteViews has a basic error, for example, forgetting to declare width or higher.

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.