在使用手機時,當有未接來電或者是新短消息時,手機會給出相應的提示信息,這些提示信息通常會顯示到手機屏幕的狀態欄上。Android也提供了用于處理此類信息的類,他們是Notification和NotificationManager。其中,Notification代表的是具有全局效果的通知;而NotificationManager則是用于發送Notification通知的系統服務。
使用Notification和NotificationManager類發送和顯示通知也比較簡單,大致可分為以下4個步驟。
(1)調用getSystemService()方法獲取系統的NotificationManager服務。
(2)創建一個Notification對象,并為其設置各種屬性
(3)為Notification對象設置事件信息
(4)通過NotificationManager類的notify()方法發送Notification通知
下面通過一個具體的實例說明如何使用Notification在狀態欄上顯示通知:
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/layout1" android:gravity="center_horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顯示通知"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="刪除通知"/> </LinearLayout>
這個是點擊通知跳轉的頁面main2.xml:
<?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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這里是詳細內容"/> </LinearLayout>
在中AndroidManifest.xml添加一下兩個權限,并在<application>標簽中注冊ContentActivity:
<!-- 添加操作閃光燈的權限 --> <uses-permission android:name="android.permission.FLASHLIGHT"/> <!-- 添加操作震動器的權限 --> <uses-permission android:name="android.permission.VIBRATE"/> <application> <activity android:name=".ContentActivity"/> </application>
MainActivity:
package com.example.test; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { public static int NOTIFYID_1=1,NOTIFYID_2=2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //獲取通知管理器,用于發送通知 final NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); Button button1=(Button) findViewById(R.id.button1);//獲取"顯示通知"按鈕 //為"顯示通知"按鈕添加單擊事件監聽器 button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Notification notify=new Notification();//創建一個Notification對象 notify.icon=R.drawable.in; notify.tickerText="顯示第一個通知"; notify.when=System.currentTimeMillis();//設置發送時間(設置為當前時間) notify.defaults=Notification.DEFAULT_ALL;//設置默認聲音、默認震動和默認閃光燈 notify.setLatestEventInfo(MainActivity.this, "無題", "每天進步一點點", null);//設置事件信息 notificationManager.notify(NOTIFYID_1,notify);//通過通知管理器發送通知 //添加第二個通知 Notification notify1=new Notification(R.drawable.music,"顯示第二個通知",System.currentTimeMillis()); notify1.flags=Notification.FLAG_AUTO_CANCEL;//打開應用程序后圖標消失 Intent intent=new Intent(MainActivity.this,ContentActivity.class);//設置為跳轉頁面準備的Intent //針對意圖的包裝對象,在下面就是通知被點擊時激活的組件對象(上下文,請求碼,意圖對象,標識符) PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, intent, 0); //設置通知的內容 (上下文對象,標題, 內容, 指定通知被點擊的時候跳轉到哪里,激活哪個組件) notify1.setLatestEventInfo(MainActivity.this, "通知", "查看詳細內容", pendingIntent); notificationManager.notify(NOTIFYID_2,notify);//通過通知管理器發送通知 } }); Button button2=(Button) findViewById(R.id.button2);//獲取"刪除通知"按鈕 //為"顯示通知"按鈕添加單擊事件監聽器 button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { notificationManager.cancel(NOTIFYID_1);//清除ID號為常量NOTIFYID_1的通知 notificationManager.cancelAll();//清除全部通知 } }); } } 運行本實例,單擊"顯示通知"按鈕,在屏幕的左上角將顯示第一個通知,如圖-4.2.2.a.jpg所示,過一段時間后,該通知消失,并顯示第二個通知,再過一段時間后,第二個通知消失,這時在狀態欄上將顯示這兩個通知的圖標,如圖-4.2.2.b.jpg所示,單擊通知圖標,將顯示如圖-4.2.2.c.jpg所示的通知列表,單擊第一個列表項,可以查看通知的詳細內容,如圖-4.2.2.d.jpg所示,查看后,該通知的圖標將不在狀態欄中顯示。單擊"刪除通知"按鈕,可以刪除全部通知。
圖-4.2.2.a.jpg:

圖-4.2.2.b.jpg:

圖-4.2.2.c.jpg:

圖-4.2.2.d.jpg:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答