實現 Android 通知提示功能
Android支持Toast和NotificationManager兩種通知方式,前者相當于一個定時關閉的對話框,后者是在狀態欄上顯示一條消息。Toast和Notification都可以隨時取消。
Toast
A toast is a view containing a quick little message for the user. The toast class helps you create and show those. Toast的使用很簡單:
Toast.makeText(this, "Service destroyed…", Toast.LENGTH_LONG).show();
NotificationManager
NotificationManager負責通知用戶事件的發生。
NotificationManager有三個公共方法:
1. cancel(int id) 取消以前顯示的一個通知.假如是一個短暫的通知,試圖將隱藏,假如是一個持久的通知,將從狀態條中移走.
2. cancelAll() 取消以前顯示的所有通知。
3. notify(int id, Notification notification) 把通知持久的發送到狀態條上.
//初始化NotificationManager:
NotificationManager nm =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification代表著一個通知.
Notification的屬性:
audioStreamType 當聲音響起時,所用的音頻流的類型
contentIntent 當通知條目被點擊,就執行這個被設置的Intent.
contentView 當通知被顯示在狀態條上的時候,同時這個被設置的視圖被顯示.
defaults 指定哪個值要被設置成默認的.
deleteIntent 當用戶點擊"Clear All Notifications"按鈕區刪除所有的通知的時候,這個被設置的Intent被執行.
icon 狀態條所用的圖片.
iconLevel 假如狀態條的圖片有幾個級別,就設置這里.
ledARGB LED燈的顏色.
ledOffMS LED關閉時的閃光時間(以毫秒計算)
ledOnMS LED開始時的閃光時間(以毫秒計算)
number 這個通知代表事件的號碼
sound 通知的聲音
tickerText 通知被顯示在狀態條時,所顯示的信息
vibrate 振動模式.
when 通知的時間戳.
Notification的公共方法:
describeContents() Describe the kinds of special objects contained in this Parcelable's marshalled representation.
setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) 設置Notification留言條的參數
writeToParcel(Parcel parcel, int flags) Flatten this notification from a parcel.
toString() …………….
將Notification發送到狀態條上:
Notification notification =new Notification(R.drawable.icon,
"Service started", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Main.class), 0);
// must set this for content view, or will throw a exception
notification.setLatestEventInfo(this,"Test Service",
"Service started", contentIntent);
nm.notify(R.string.hello, notification);
Notification的取消
nm.cancel(R.string.hello);
package wzhnsc.testnotification; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; public class Main extends Activity { NotificationManager objNotificationManager; Notification objNotification; PendingIntent objPendingIntent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 聲明通知(消息)管理器只要知道它是用來管理通知消息的就行了 objNotificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE ); int icon = R.drawable.icon; CharSequence tickerText = "小圖標旁的文字"; objNotification = new Notification( icon, tickerText, System.currentTimeMillis() ); // 如果有振動或者全部提示方式,必須在 AndroidManifest.xml 加入振動權限 objPendingIntent = PendingIntent.getActivity( this, 0, new Intent( this, Main.class ), 0 ); objNotification.setLatestEventInfo( this, "通知欄的標題", "通知欄的內容", objPendingIntent ); // 添加入通知管理器中 objNotificationManager.notify( 0, objNotification ); } }