Android應用開發基礎篇(2)—–Notification(狀態列通知)

來源:互聯網
上載者:User

一、概述

     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>

好了,運行程式後,如下

 點擊按鈕後,可以看到狀態列裡顯示一個訊息:

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

 

 

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

 

 要求完成!

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.