Android應用開發基礎篇(2)-----Notification(狀態欄通知)


一、概述

     Notification這個部件的功能是在狀態欄里顯示消息提醒,比如有未讀的短信或者是未接的電話,那么狀態欄里都會有顯示,更或者是從某個應用(比如QQ,酷我音樂等等)里按Home鍵回到桌面,這時狀態欄里也會顯示這個應用的圖標,這就是Notification。

 

二、要求

     程序主界面上有一個Button按鈕,當用戶點擊這個按鈕時狀態欄會顯示一則通知,當按住狀態欄下拉時可以看到這個通知在下拉列表里,此時點擊這個通知就跳轉到另一個界面(相當於查看這個通知)並且能將這個通知在狀態欄里取消。

 

三、實現

      新建工程MyNotice,在/res/layout/main.xml文件里添加一個Button:

 

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

完整的main.xml文件:

 

<?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>

修改后的MyNoticeActivity.java文件

 1 package com.nan.notice;
2
3 import android.app.Activity;
4 import android.app.Notification;
5 import android.app.NotificationManager;
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 //通知的編號
16 static final int MYNOTICE = 0;
17
18 //定義各個對象
19 private Button mButton = null;
20 private NotificationManager mNotificationManager = 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實例化
34 mButton = (Button)findViewById(R.id.mbutton);
35 //mNotificationManager實例化
36 mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
37
38 mIntent = new Intent();
39 //設置要跳轉到的Activity
40 mIntent.setClass(MyNoticeActivity.this, Activity2.class);
41 //設置點擊下拉狀態欄列表里的這個通知時所要顯示的Activity
42 mPendingIntent = PendingIntent.getActivity(MyNoticeActivity.this, 0, mIntent, 0);
43 mNotification = new Notification();
44 //設置在通知欄里顯示的圖標
45 mNotification.icon = R.drawable.ic_launcher;
46 //設置在通知欄里顯示的文本
47 mNotification.tickerText = "Button 通知...";
48 //設置通知鈴聲
49 mNotification.defaults = Notification.DEFAULT_SOUND;
50 //設置在下拉狀態欄時所顯示的關於這個通知的內容
51 mNotification.setLatestEventInfo(MyNoticeActivity.this, "Button", "Button通知", mPendingIntent);
52 //設置按鈕監聽
53 mButton.setOnClickListener(new View.OnClickListener()
54 {
55 @Override
56 public void onClick(View v)
57 {
58 // TODO Auto-generated method stub
59 //執行這個通知
60 mNotificationManager.notify(MYNOTICE, mNotification);
61
62 }
63 });
64
65 }
66
67 }

在/src里添加一個名為Activity2.java文件:

 1 package com.nan.notice;
2
3 import android.app.Activity;
4 import android.app.NotificationManager;
5 import android.os.Bundle;
6
7
8 public class Activity2 extends Activity
9 {
10 //與MyNoticeActivity.java中定義的值相同
11 static final int MYNOTICE = 0;
12
13 private NotificationManager mNotificationManager = 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 mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
23
24 }
25
26 @Override
27 public void onResume()
28 {
29 super.onResume();
30 //在Activity顯示完成后取消在狀態欄里的這個通知
31 mNotificationManager.cancel(MYNOTICE);
32 }
33
34 }

在/res/layout里添加一個activity2.xml文件

 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>

在AndroidManifest.xml文件里聲明多一個activity

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

好了,運行程序后,如下

 點擊按鈕后,可以看到狀態欄里顯示一個消息:

 按住狀態欄然后下拉,可以看到有一條提示:

 

 

 點擊這條提示,進入到這條提示的內容,同時狀態欄里的這個通知也消失了:

 

 要求完成!

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM