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);
顯示效果:編程
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);
顯示效果:ui
這裏筆者以點擊後打電話爲例this