Simple use of Status Bar Notification

Source: Internet
Author: User

Today, when I was studying Notification, I also referred to some related blogs. Now I will summarize them based on my own learning practices.

When using a mobile phone, when there is a missed call or short message, it is usually displayed on the status bar on the mobile phone screen. AlertDialog can also be used for reminders in Android. However, when AlertDialog is used, ongoing operations will be interrupted. Because the current focus is obtained by AlertDialog. We can imagine that a text message was sent when a user played a game well. If AlertDialog is used as a reminder, you must handle this reminder before continuing the game. The user may be angry. However, using Notification will not cause these troubles. Users can play the game and then read this text message. Therefore, select appropriate controls based on actual needs during development.

 

Status bar notifications involve two classes: Notification, which represents one Notification, and NotificationManager, which is the system service used to send Notification.

Generally, there are four steps to use status bar notifications:

1. Use the getSystemService () method to obtain the icationicationmanager service.

2. Create a Notification object and set various attributes for it.

3. Set event information for the Notification object.

4. Send the notification to the status bar through icationicationmanager's notify () method.

The following is an example for actual operations:

First, use the layout file new_main.xml:

View Code

After creating an Activity jump, the page content. xml required by the new Activity is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <android: id = "@ + id/textView1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "test" android: textAppearance = "? Android: attr/textAppearanceMedium "/> </LinearLayout>View Code

Create a ContentActivity (the page to jump to after clicking the notification ):

ContentActivity}View Code

Define two variables in MainActivity:

  NOTIFYID_1 = 123;   NOTIFYID_2 = 124; 

Then rewrite the onCreate () method:

Icationicationmanager icationmanager = Button button1 = button1.setOnClickListener (Notification inform y = policy. icon = policy. tickerText = "you have a new message! "Y. when = System. currentTimeMillis (); notify. defaults = Notification. DEFAULT_ALL; notify. setLatestEventInfo (MainActivity.," Notice 1 "" This afternoon no class! ", Notification completed Y1 = Notification (R. drawable. ic_launcher, "another text message" Intent intent = Intent (MainActivity. contentActivity. pendingIntent pendingIntent = MainActivity ., 0, intent, 0 policy1.flags | = policy1.setlatesteventinfo (MainActivity ., "Notification 2", "click to View Details" Button button2 = button2.setOnClickListener (icationicationmanager. cancelAll ();}View Code

Finally, add the relevant code to the configuration file:

<Uses-permission android: name = "android. permission. FLASHLIGHT "/> <uses-permission android: name =" android. permission. VIBRATE "/> <activity android: name =" com. topcsa. demo_notification.ContentActivity "=" notification "=" @ android: style/Theme. dialog "/>

Well, a simple Demo is ready.

It is worth noting that there are abandoned methods in the code of the two notifications, which will be dropped by a black line in eclipse. The related documents are:

 

We know that the above method and constructor have been abandoned in Android API 11. Let's take a look at its recommended method:

The MainActivity of the above instance contains the code for implementing this recommendation method (which requires uncommenting). When using the new recommendation code, please note the following code:

Notification Y1 = "another text message", "Notification 2", "click to View Details"

Run the program and the recommended method is used.

 

After the end of this article saw a good article: http://blog.csdn.net/loongggdroid/article/details/17616509

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.