要使用消息通知,必需要用到兩個類:NotificationManager和Notification,其餘NotificationManager的初始化是用getSystemService方法,而且經過notify方法來向android系統發送消息欄通知和顯示。android
效果:this
代碼:get
//消息通知欄it
//定義NotificationManager
io
String ns = Context.NOTIFICATION_SERVICE;class
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);test
//定義通知欄展示的內容信息方法
int icon = R.drawable.icon;im
CharSequence tickerText = "個人通知欄標題";notification
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
//定義下拉通知欄時要展示的內容信息
Context context = getApplicationContext();
CharSequence contentTitle = "個人通知欄標展開標題";
CharSequence contentText = "個人通知欄展開詳細內容";
Intent notificationIntent = new Intent(this, BootStartDemo.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,contentIntent);
//用mNotificationManager的notify方法通知用戶生成標題欄消息通知
mNotificationManager.notify(1, notification);