第一行代码中的代码是基于android 7.0进行编写,API的调用已经落后较多
Notification 常用写法:
private void showNotification() {
Intent intent = FragmentTestActivity.formatIntent(this);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setContentTitle("title")
.setContentText("test")
.setWhen(System.currentTimeMillis()) //show time on notification
.setSmallIcon(R.mipmap.ic_launcher_round) //icon in the status bar
.setAutoCancel(true) //cancel self when click notification
.setContentIntent(pi) //set the intent handled when click notification
.build();
if(notiManager != null){
notiManager.notify(1, notification);
}
}
手动关闭notification:
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if(nm != null){
//1 is the id, which should be the same int value when `notify(int id, Notification notification) called`
nm.cancel(1);
}
NotificationChannel: 8.0 新增特性
下午学习