1. 對於高版本的sdk, (16以上)
1 //高版本的通知欄,最低要求sdk版本為16 2 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 3 4 //鏈式編程,每次返回的都是一個builder對象 5 Notification notification = new Notification.Builder(this) 6 .setContentTitle("標題") 7 .setContentText("內容") 8 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) 9 .setSmallIcon(R.drawable.ic_launcher) 10 .build(); 11 nm.notify(1, notification);
顯示效果:
2. 對於低版本的sdk
1 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 2 3 //第二個參數為:在狀態欄上翻動顯示的文本 4 Notification notification = new Notification(R.drawable.ic_launcher, "出來了?", System.currentTimeMillis()); 5 6 //指定點擊通知之后,跳轉一個界面,以打電話為例 7 Intent intent = new Intent(); 8 intent.setAction(Intent.ACTION_CALL); 9 intent.setData(Uri.parse("tel://110")); 10 11 // 延期的意圖對象 ---用於描述 將來干什么事兒 12 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 13 14 //設置拖動通知下來之后,展示的內容以及點擊之后跳轉到的界面 15 notification.setLatestEventInfo(this, "標題", "內容", contentIntent ); 16 17 nm.notify(1, notification);
顯示效果:
這里筆者以點擊后打電話為例