今天總結了一下Notification的使用,發現這個控件在版本上有點扯淡。API11和API16兩個分水嶺,致使菜鳥在學習的時候比較吃力,受影響的外界因素是多樣的,其中比較重要的就是你測試的android設備的版本,我剛開始使用的android版本爲android4.0.4,我在程序中使用的是API16以上的方法,正好android4.0對應的API爲API15,相差了一個版本,致使發出通知的時候程序掛掉。html
若是你要兼容低版本的android系統的話(API11如下),建議使用一下方法:android
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 建立一個PendingIntent,和Intent相似,不一樣的是因爲不是立刻調用,須要在下拉狀態條出發的activity,因此採用的是PendingIntent,即點擊Notification跳轉啓動到哪一個Activity PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); // 下面需兼容Android 2.x版本是的處理方式 // Notification notify1 = new Notification(R.drawable.message,"TickerText:" + "您有新短消息,請注意查收!", System.currentTimeMillis()); Notification notify1 = new Notification(); notify1.icon = R.drawable.ic_launcher; notify1.tickerText = "TickerText:您有新短消息,請注意查收!"; notify1.number = 2; notify1.when = System.currentTimeMillis(); notify1.setLatestEventInfo(this, "Notification Title","This is the notification message", pendingIntent); notify1.flags |= Notification.FLAG_NO_CLEAR; // FLAG_AUTO_CANCEL代表當通知被用戶點擊時,通知將被清除。 // 經過通知管理器來發起通知。若是id不一樣,則每click,在statu那裏增長一個提示 manager.notify(NOTIFICATION_FLAG, notify1);
若是你要兼容Android3.0及其以上版本(API11)系統的話,請使用如下方法: git
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0,new Intent(this, MainActivity.class), 0); // 經過Notification.Builder來建立通知,注意API Level // API11以後才支持 Notification notify2 = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_launcher) // 設置狀態欄中的小圖片,尺寸通常建議在24×24,這個圖片一樣也是在下拉狀態欄中所顯示,若是在那裏須要更換更大的圖片,可使用setLargeIcon(Bitmap icon) .setTicker("TickerText:" + "您有新短消息,請注意查收!")// 設置在status bar上顯示的提示文字 .setContentTitle("Notification Title")// 設置在下拉status bar後Activity,本例子中的NotififyMessage的TextView中顯示的標題 .setContentText("This is the notification message")// TextView中顯示的詳細內容 .setContentIntent(pendingIntent2) // 關聯PendingIntent .setNumber(1) // 在TextView的右方顯示的數字,可放大圖片看,在最右側。這個number同時也起到一個序列號的左右,若是多個觸發多個通知(同一ID),能夠指定顯示哪個。 .getNotification(); // 須要注意build()是在API level 16及以後增長的,在API11中可使用getNotificatin()來代替 notify2.flags |= Notification.FLAG_AUTO_CANCEL; manager.notify(NOTIFICATION_FLAG, notify2);
若是你只考慮Android4.1及其以上版本的話,請使用如下方法比較好github
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 默認通知 API16及以後可用 PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); // 經過Notification.Builder來建立通知,注意API Level (API16以後才支持) Notification notify3 = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setTicker("TickerText:" + "您有新短消息,請注意查收!") .setContentTitle("Notification Title") .setContentText("This is the notification message") .setContentIntent(pendingIntent3).setNumber(1).build(); // 須要注意build()是在API level16及以後增長的,API11可使用getNotificatin()來替代 notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL代表當通知被用戶點擊時,通知將被清除。 manager.notify(NOTIFICATION_FLAG, notify3); // 步驟4:經過通知管理器來發起通知。若是id不一樣,則每click,在status哪裏增長一個提示
自定義界面的Notification,這個界面有點簡單了(僅有一個textView)。學習
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification myNotify = new Notification(); myNotify.icon = R.drawable.ic_launcher; myNotify.tickerText = "TickerText:您有新短消息,請注意查收!"; myNotify.when = System.currentTimeMillis(); myNotify.flags = Notification.FLAG_NO_CLEAR;// 不可以自動清除 RemoteViews rv = new RemoteViews(getPackageName(),R.layout.my_notification); rv.setTextViewText(R.id.text_content, "hello wrold!"); myNotify.contentView = rv; Intent intent = new Intent(Intent.ACTION_MAIN); PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, 1); myNotify.contentIntent = contentIntent; manager.notify(NOTIFICATION_FLAG, myNotify);
咱們在須要的時候還要清除通知,若是通知一直顯示的話總不是太好測試
// 清除id爲NOTIFICATION_FLAG的通知 manager.cancel(NOTIFICATION_FLAG); // 清除全部的通知 manager.cancelAll();
參考網址: Android之Notification的多種用法:http://blog.csdn.net/loongggdroid/article/details/17616509this
最近剛剛發現一篇寫的比較好的介紹Notification的文章!spa
Android API Level在11先後及16以後時Notification的不一樣用法: http://www.cnblogs.com/hasse/p/5164417.html.net