lijianqiang12
12/24/2017 - 5:53 AM

android8.0以后,通知需要单独适配。

android8.0以后,通知需要单独适配。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      String channelId = "my_channel";
      String channelName = "My Channel";
      NotificationChannel chan = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
      chan.setLightColor(Color.BLUE);
      chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
      NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      service.createNotificationChannel(chan);
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
      Notification notification = notificationBuilder.setOngoing(true)
//              .setSmallIcon(R.drawable.ic_launcher_foreground)
              .setCategory(Notification.CATEGORY_SERVICE)
              .setPriority(Notification.PRIORITY_MIN)
              .build();
      startForeground(1234, notification);//该方法已创建通知管理器,设置为前台优先级后,点击通知不再自动取消
    } else {
      Notification.Builder mBuilder = new Notification.Builder(this)
              .setSmallIcon(R.drawable.ic_launcher)
              .setContentTitle("远离手机")
              .setContentText("定时锁机运行中")
//            .setContentIntent(contentIntent)
              .setPriority(Notification.PRIORITY_MIN);
      Notification notification = mBuilder.build();
      startForeground(1234, notification);//该方法已创建通知管理器,设置为前台优先级后,点击通知不再自动取消
    }