1.介紹
通知是android系統中比較有特色的功能之一,當某個應用程序希望向用戶發出一些提示信息的時候,而應用程序又不在前臺運行,可以藉助通知來實現。發出一條通知後手機通知欄會顯示一條通知消息。
通知可以在活動、廣播、服務裏創建。雖然通知在活動裏創建比較少,但是爲了演示方便還是選擇了在活動裏創建。
2.舉例
2.1 發送通知
點擊發送通知。
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("標題")//標題
.setContentText("內容")//內容
.setWhen(System.currentTimeMillis())//通知被創建的時間
.setSmallIcon(R.mipmap.ic_launcher)//小圖標
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大圖標
.build();
manager.notify(1, notification);//id 1
僅僅是發送一條通知。
2.2發送通知點擊通知跳轉
當收到通知後,點擊通知跳轉到對應的目錄
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
//第一個參數 上下文
//第二個參數 默認0
//第三個參數 Intent
//第四個參數 行爲 FLAG_ONE_SHOT FLAG_NO_CARATE FLAG_CANCEL_CURRENT FLAG_UPDATE_CURRENT
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("標題")//標題
.setContentText("內容")//內容
.setWhen(System.currentTimeMillis())//通知被創建的時間
.setSmallIcon(R.mipmap.ic_launcher)//小圖標
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大圖標
.setContentIntent(pendingIntent)//點擊挑轉
.build();
manager.notify(2, notification);//id 2
跳轉完了,通知還保留着。
2.3清除通知
清除通知有兩種方法,一種是 點擊通知後自動取消:
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
//第一個參數 上下文
//第二個參數 默認0
//第三個參數 Intent
//第四個參數 行爲 FLAG_ONE_SHOT FLAG_NO_CARATE FLAG_CANCEL_CURRENT FLAG_UPDATE_CURRENT
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("標題")//標題
.setContentText("內容")//內容
.setWhen(System.currentTimeMillis())//通知被創建的時間
.setSmallIcon(R.mipmap.ic_launcher)//小圖標
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大圖標
.setContentIntent(pendingIntent)//點擊挑轉
.setAutoCancel(true)//點擊通知後自動取消 或者從跳轉的activity取消
.build();
manager.notify(3, notification);//id 3
另外一種是跳轉到對應的活動後取消:
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
//第一個參數 上下文
//第二個參數 默認0
//第三個參數 Intent
//第四個參數 行爲 FLAG_ONE_SHOT FLAG_NO_CARATE FLAG_CANCEL_CURRENT FLAG_UPDATE_CURRENT
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("標題")//標題
.setContentText("內容")//內容
.setWhen(System.currentTimeMillis())//通知被創建的時間
.setSmallIcon(R.mipmap.ic_launcher)//小圖標
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大圖標
.setContentIntent(pendingIntent)//點擊挑轉
.build();
manager.notify(4, notification);//id 4
對應的Main2Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// manager.notify(4, notification);//id 4
manager.cancel(4);//
}
中的 manager.cancel(4);4就是是在跳轉的時候配置的id。
2.4播放音樂
播放本地的音樂
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("標題")//標題
.setContentText("薛之謙-怪咖-最好")//內容
.setWhen(System.currentTimeMillis())//通知被創建的時間
.setSmallIcon(R.mipmap.ic_launcher)//小圖標
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大圖標
.setSound(Uri.fromFile(new File("/storage/emulated/0/Baidu_music/download/薛之謙-怪咖-最好-128.mp3")))
.build();
manager.notify(5, notification);//id 5
2.5伴隨震動
當收到通知的時候震動
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("標題")//標題
.setContentText("震動")//內容
.setWhen(System.currentTimeMillis())//通知被創建的時間
.setSmallIcon(R.mipmap.ic_launcher)//小圖標
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大圖標
.setVibrate(new long[]{0, 1000, 1000, 1000})//靜止 0 ;震動 1000;靜止1000;震動1000
.build();
manager.notify(6, notification);//id 6
2.6伴隨提示燈
當收到通知的時候鎖屏狀態下顯示呼吸燈
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("標題")//標題
.setContentText("燈")//內容
.setWhen(System.currentTimeMillis())//通知被創建的時間
.setSmallIcon(R.mipmap.ic_launcher)//小圖標
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大圖標
.setLights(Color.GREEN, 1000, 1000)//燈的顏色 亮起時長,暗去時長
.build();
manager.notify(7, notification);//id 7
2.7跟隨系統默認
默認和系統一致,震動、呼吸燈
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("標題")//標題
.setContentText("默認")//內容
.setWhen(System.currentTimeMillis())//通知被創建的時間
.setSmallIcon(R.mipmap.ic_launcher)//小圖標
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大圖標
.setDefaults(NotificationCompat.DEFAULT_ALL)//根據手機的環境來決定
.build();
manager.notify(8, notification);//id 8
2.8顯示長文本
當通知文本長的時候顯示不全,顯示全部文本。
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("標題")//標題
// .setContentText("長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本123")//內容
.setWhen(System.currentTimeMillis())//通知被創建的時間
.setSmallIcon(R.mipmap.ic_launcher)//小圖標
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大圖標
.setDefaults(NotificationCompat.DEFAULT_ALL)//根據手機的環境來決定
.setStyle(new NotificationCompat.BigTextStyle().bigText("長文本長文本長文本長文本長" +
"文本長文本長文本長文本長文本長文 本長文本長文本長文本長文本長文本長文本長文" +
"本長文本長文本長文本長文本長文本長文本123"
))
.build();
manager.notify(9, notification);//id 9
2.9顯示大圖
加載通知中的大圖
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("標題")//標題
// .setContentText("長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本長文本123")//內容
.setWhen(System.currentTimeMillis())//通知被創建的時間
.setSmallIcon(R.mipmap.ic_launcher)//小圖標
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大圖標
.setDefaults(NotificationCompat.DEFAULT_ALL)//根據手機的環境來決定
.setStyle(new NotificationCompat.BigTextStyle().bigText("長文本長文本長文本長文本長" +
"文本長文本長文本長文本長文本長文 本長文本長文本長文本長文本長文本長文本長文" +
"本長文本長文本長文本長文本長文本長文本123"
))
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.
decodeResource(getResources(),R.drawable.pig)))
.build();
manager.notify(10, notification);//id 10
2.10最高優先級顯示
顯示在最上面
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("標題")//標題
.setContentText("最高優先級")//內容
.setWhen(System.currentTimeMillis())//通知被創建的時間
.setSmallIcon(R.mipmap.ic_launcher)//小圖標
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大圖標
// .setDefaults(NotificationCompat.DEFAULT_ALL)//根據手機的環境來決定
.setPriority(NotificationCompat.PRIORITY_MAX)
.build();
manager.notify(11, notification);//id 11
轉發表明出處:https://blog.csdn.net/qq_35698774/article/details/107051528
點擊下載源碼
android互助羣:
感謝:郭霖的《第一行代碼 第二版》