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