android 通知欄調用
使用到的類包括
NotificationManager
Notification
NotificationManager 是系統自身的通知服務, 可以通過 (NotificationManager)Context.getSystemService(Context.NOTIFICATION_SERVICE) 獲得
這個類中我們只使用到一個API
void android.app.NotificationManager.notify(int id, Notification notification)
id An identifier for this notification unique within your application. //就是用來在本身程序中標識Notification的
再說Notification
Notification 就是我們的通知.
這個類有三個構造函數
Notification()
Constructs a Notification object with everything set to 0.
Notification(int icon, CharSequence tickerText, long when)
Constructs a Notification object with the information needed to have a status bar icon without the standard expanded view.
Notification(Parcel parcel)
Unflatten the notification from a parcel.
第二個的參數
icon 就是 我們想在通知欄上顯示的, 還不是拉開通知欄之后的圖標
tickerText 也是在通知欄上顯示的字體
when 是在什么時候顯示
類中的成員變量
以下幾個常量, 主要用于設置 成員變量 defaults
int DEFAULT_ALL Use all default values (where applicable).
int DEFAULT_LIGHTS Use the default notification lights. //提示燈
int DEFAULT_SOUND Use the default notification sound. //提示音
int DEFAULT_VIBRATE Use the default notification vibrate. //震動
我們來看 defaults
public int defaults
Specifies which values should be taken from the defaults.
To set, OR the desired from DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS. For all default values, use DEFAULT_ALL.
這個意思就是, 要么你直接賦值 DEFAULT_ALL, 要么 就使用或將幾個常量串起來給這個變量賦值
int FLAG_AUTO_CANCEL Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user. // 自動刪除
int FLAG_FOREGROUND_SERVICE Bit to be bitwise-ored into the flags field that should be set if this notification represents a currently running service. // 表示一個服務正在運行
int FLAG_INSISTENT Bit to be bitwise-ored into the flags field that if set, the audio will be repeated until the notification is cancelled or the notification window is opened.
int FLAG_NO_CLEAR Bit to be bitwise-ored into the flags field that should be set if the notification should not be canceled when the user clicks the Clear all button. //真無賴, 點清除也沒辦法清除
int FLAG_ONGOING_EVENT Bit to be bitwise-ored into the flags field that should be set if this notification is in reference to something that is ongoing, like a phone call.
int FLAG_ONLY_ALERT_ONCE Bit to be bitwise-ored into the flags field that should be set if you want the sound and/or vibration play each time the notification is sent, even if it has not been canceled before that.
int FLAG_SHOW_LIGHTS Bit to be bitwise-ored into the flags field that should be set if you want the LED on for this notification. //顯示提示燈
int STREAM_DEFAULT Use this constant as the value for audioStreamType to request that the default stream type for notifications be used.
我們來看下一些需要設置的公共成員
public int audioStreamType The audio stream type to use when playing the sound.
//當被點擊, 啟動的Intent
public PendingIntent contentIntent The intent to execute when the expanded status entry is clicked.
//拉下通知欄后, 顯示 也就是通知的顯示
public RemoteViews contentView The view that shows when this notification is shown in the expanded status bar.
public int defaults Specifies which values should be taken from the defaults.
public PendingIntent deleteIntent The intent to execute when the status entry is deleted by the user with the "Clear All Notifications" button.
public int flags
public int icon The resource id of a drawable to use as the icon in the status bar.
public int iconLevel If the icon in the status bar is to have more than one level, you can set this.
public int ledARGB The color of the led.
public int ledOffMS The number of milliseconds for the LED to be off while it's flashing.
public int ledOnMS The number of milliseconds for the LED to be on while it's flashing.
public int number The number of events that this notification represents.
public Uri sound The sound to play.
public CharSequence tickerText Text to scroll across the screen when this item is added to the status bar.
public long[] vibrate The pattern with which to vibrate.
public long when The timestamp for the notification.
setLatestEventInfo 就是設置contentview中一些顯示
有些東西 看看英文也知道了
//例1
NotificationManager mg =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification ntf = new Notification(
R.drawable.icon, "通知欄", System.currentTimeMillis());
PendingIntent pIntent = PendingIntent.getActivity(SetRingtone.this, 0, new Intent(SetRingtone.this, SetRingtone.class), 0);
ntf.setLatestEventInfo(getApplicationContext(),
"contentTitle", "content text", pIntent);
ntf.flags |= Notification.FLAG_AUTO_CANCEL;
mg.notify(0, ntf);
//例2
nf =new Notification(R.drawable.icon,"帶進度條的提醒",System.currentTimeMillis()) ;
nf.icon = R.drawable.icon;
nf.contentView= new RemoteViews(this.getPackageName(),R.layout.notification);
nf.contentView.setProgressBar(R.id.ProgressBar01, 100, 0, false);
nf.contentIntent=PendingIntent.getActivity( this, 0, new Intent(this,remoteview.class) ,0);
參考文章
http://hemowolf.javaeye.com/blog/604133
http://www.hlovey.cn/2009/09/10/android-notification-message.html
http://marshal.easymorse.com/archives/2960