安卓8.0系统和8.0以下系统显示弹出通知状态栏方法。
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//创建notification
builder = new Notification.Builder(this)
.setOngoing(true)//正在运行
.setWhen(System.currentTimeMillis())//什么时候弹出通知栏
.setContentTitle("title")
.setContentText("描述")
.setAutoCancel(false)//是否自动消失
.setSmallIcon(R.mipmap.logoz);//设置小图标
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//8.0以上弹出通知状态栏
String channelID = "0";
String channelName = "channel_name";
NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
builder.setChannelId(channelID);
builder.build();
}
}else {
//弹出通知栏 8.0以下系统弹出方式
if (notificationManager != null) {
notificationManager.notify(0, builder.build());
}
}
如果targetSdkVersion适配到26的话,也就是8.0系统。在全量更新apk的时候, 别忘记在清单文件里面添加
<!--安卓8.0系统安装app 未知来源权限-->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />