Android(安卓)8.0 通知栏采坑日记
Android 8.0 通知栏的详细问题可以参考此篇博客:
https://blog.csdn.net/guolin_blog/article/details/79854070
此篇博客仅为个人日后回忆所用:
首先在8.0以后 通知栏的使用 需要与NotificationChannel配合着使用:
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
channelId,channelName的话都是字符串,channelid需要有唯一性(如果是重复的,在通知栏里面就会给覆盖掉)。
importance是表示设置通知的优先级:
NotificationManager.IMPORTANCE_NONE 关闭通知
NotificationManager.IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示
NotificationManager.IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
NotificationManager.IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
NotificationManager. IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示
然后是设置一些属性:
channel.enableLights(true);//设置闪光灯,需要硬件支持
channel.setLightColor(Color.RED);//设置闪光灯颜色
//如果uri地址使用的是资源文件raw目录下的铃声 可以这么些
sound= Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.dong );
channel.setSound(sound, Notification.AUDIO_ATTRIBUTES_DEFAULT);//设置声音 sound 是uri
channel.enableVibration(true);//允许震动
channel.setVibrationPattern(new long[]{100,100});//震动频率设置
channel.setShowBadge(true);
最后必须调用:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
然后继续:
Notification notification=new Notification.Builder(MainActivity.this)//个方法还有个传入context,channelid的构造方法,不过也是在8.0以上版本使用,如果使用需注意适配
Notification notification=new Notification.Builder(MainActivity.this) .setAutoCancel(true)//设置点击后是否自动关闭 .setChannelId(chaid)//上面设置的id .setContentTitle("新消息来了") .setContentText("明天真的是周六") .setSmallIcon(R.mipmap.ic_launcher) .build();
manager.notify(i,notification);//这个方法第一个参数是一个int类型 也应该是全局唯一,避免通知栏被相同id覆盖
最基本的通知也就是如此了,记录下以后使用
更多相关文章
- Android(安卓)5个进程等级
- Java乔晓松-android控制显示和隐藏视图或控件的操作
- Android(安卓)应用软件开发(十四)WIFI
- Android学习笔记-ProgressBar和ListView使用方法(一)
- Android——permission 列表
- android permission中英文大全
- Android_UI_全屏去掉标题栏和状态栏的两种方法
- android 8.0后打开wifi热点以及更改热点帐号密码,打开移动数据的
- Android通知系统