Android Application Development BASICS (2) -- Notification (status bar notification)

Source: Internet
Author: User

I. Overview

The function of notification is to display message reminders in the status bar. For example, if there are unread text messages or missed calls, the status bar will be displayed, or from an application (such as QQ, in cool music, etc.), press the Home Key to return to the desktop. Then, the icon of this application will be displayed in the status bar, which is notification.

 

Ii. Requirements

There is a button on the main interface of the program. When you click this button, a notification is displayed in the status bar. When you press and hold the status bar drop-down, you can see this notification in the drop-down list, click this notification to go to another page (equivalent to viewing the notification) and cancel the notification in the status bar.

 

Iii. Implementation

Create a project mynotice and add a button in the/RES/layout/Main. xml file:

 

 <Button
android:id="@+id/mbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Notice"
/>

Complete main. xml file:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<Button
android:id="@+id/mbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Notice"
/>

</LinearLayout>

Modified mynoticeactivity. Java File

1 package com. Nan. Notice;
2
3 Import Android. App. activity;
4 Import Android. App. notification;
5 import Android. App. icationicationmanager;
6 Import Android. App. pendingintent;
7 Import Android. content. intent;
8 Import Android. OS. Bundle;
9 Import Android. View. view;
10 Import Android. widget. Button;
11
12
13 public class mynoticeactivity extends Activity
14 {
15 // notification ID
16 static final int mynotice = 0;
17
18 // define objects
19 private button mbutton = NULL;
20 private icationicationmanager micationicationmanager = NULL;
21 private intent mintent = NULL;
22 private notification mnotification = NULL;
23 private pendingintent mpendingintent = NULL;
24
25
26/** called when the activity is first created .*/
27 @ override
28 public void oncreate (bundle savedinstancestate)
29 {
30 Super. oncreate (savedinstancestate );
31 setcontentview (R. layout. Main );
32
33 // mbutton instantiation
34 mbutton = (button) findviewbyid (R. Id. mbutton );
35 // micationicationmanager instantiation
36 micationicationmanager = (notificationmanager) getsystemservice (notification_service );
37
38 mintent = new intent ();
39 // set the activity to jump
40 mintent. setclass (mynoticeactivity. This, activity2.class );
41 // set the activity to be displayed when you click the notification in the drop-down status bar list
42 mpendingintent = pendingintent. getactivity (mynoticeactivity. This, 0, mintent, 0 );
43 mnotification = new notification ();
44 // set the icon displayed in the notification bar
45 mnotification. Icon = R. drawable. ic_launcher;
46 // set the text displayed in the notification bar
47 mnotification. tickertext = "button notification ...";
48 // set the notification ringtone
49 mnotification. defaults = notification. default_sound;
50 // set the content of the notification displayed in the drop-down Status Bar
51 mnotification. setlatesteventinfo (mynoticeactivity. This, "button", "button notification", mpendingintent );
52 // set button listening
53 mbutton. setonclicklistener (New View. onclicklistener ()
54 {
55 @ override
56 public void onclick (view V)
57 {
58 // todo auto-generated method stub
59 // execute this notification
60 mnotificationmanager. Notify (mynotice, mnotification );
61
62}
63 });
64
65}
66
67}

Add a file named activity2.java in/src:

1 package com. Nan. Notice;
2
3 Import Android. App. activity;
4 Import Android. App. icationicationmanager;
5 import Android. OS. Bundle;
6
7
8 public class activity2 extends Activity
9 {
10 // same as the value defined in mynoticeactivity. Java
11 static final int mynotice = 0;
12
13 private icationicationmanager micationicationmanager = NULL;
14
15/** called when the activity is first created .*/
16 @ override
17 Public void oncreate (bundle savedinstancestate)
18 {
19 super. oncreate (savedinstancestate );
20 setcontentview (R. layout. activity2 );
21
22 micationicationmanager = (notificationmanager) getsystemservice (icationication_service );
23
24}
25
26 @ override
27 public void onresume ()
28 {
29 super. onresume ();
30 // cancel the notification in the status bar after the activity is displayed
31 mnotificationmanager. Cancel (mynotice );
32}
33
34}

Add an activity2.xml file to/RES/layout.

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="Button Notificition"
11 android:textSize="30px"
12 />
13
14 </LinearLayout>

Declare one more activity in the androidmanifest. xml file

<activity
android:name=".Activity2"
>
</activity>

Okay. After running the program

Click the button to view a message in the status bar:

Press and hold the status bar and then drop down. You can see a prompt:

 

 

Click this prompt to enter the content of this prompt, and the notification in the status bar disappears:

 

 Requirement complete!

 

 

 

 

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.