private Notification buildNotification() {
String channelId;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channelId = "GaoDeLocationService";
String channelName = "GaoDeLocationService";
createNotificationChannel(channelId, channelName);
} else {
channelId = "";
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
NotificationCompat.Builder builder =
notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.icon_vas_logo)
.setVibrate(null)
.setSound(null)
.setContentTitle("大虾师傅")
.setContentText("正在后台运行")
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setPriority(NotificationCompat.PRIORITY_MAX);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setCategory(Notification.CATEGORY_SERVICE);
}
return builder.build();
}
@RequiresApi(Build.VERSION_CODES.O)
private void createNotificationChannel(String channelId, String channelName) {
NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (service == null || service.getNotificationChannel(channelId) != null) {
return;
}
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.enableVibration(false);
chan.setVibrationPattern(new long[]{0});
chan.setSound(null, null);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
service.createNotificationChannel(chan);
}
NotificationCompat 助力显示锁屏通知 和设置通知为最重要
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 【蝴蝶效应】 蝴蝶效应:上个世纪70年代,美国一个名叫洛伦兹的气象学家在解释空气系统理论时说,亚马逊雨林一只蝴蝶...
- 新建Activity,并在activity的onCreate()方法中设置flags 在AndroidManife...
- 网上关于这个的使用有很多,我就不做那么多的介绍了,说说5.0以后的锁屏通知栏显示和页面的悬浮通知显示。Notifi...
- 步骤一:打开我们的系统设置中心 步骤二:打开通知管理 步骤三:点击锁屏通知 最后点击显示规则,然后在底部的筛选列表...