上一篇主要讲的是Notification 的 普通视图 以及 自定义的视图,本篇继续介绍其另外两种视图- 宽视图、锁屏视图、悬挂视图
普通视图详解链接
一、宽视图
android 提供的Notification 宽视图有:
BigTextStyle: 显示一个大的文字块
BigPictureStyle: 详情区包含一个256dp的图片
InboxStyle: 收件箱风格:显示多行文字
1. BigTextStyle
NotificationCompat.BigTextStyle textStyle = new NotificationCompat.BigTextStyle(noBuil);
// textStyle.setBuilder(noBuil); 此句和上面构造传入的参数意思一致
其存在的一些方法,结合代码与效果图比较明显
setBigContentTitle: 标题
bigText: 文本信息
setSummaryText: 最下面的文字信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14Intent launchIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launchIntent, 0); NotificationCompat.Builder noBuil = new NotificationCompat.Builder(this); noBuil.setSmallIcon(R.drawable.feedback_person); noBuil.setContentTitle("ContentTitle"); noBuil.setContentText("This is a ContentText"); noBuil.addAction(R.drawable.ic_details_collect_focus, "嗯哼", contentIntent); noBuil.addAction(R.drawable.ic_details_collect_focus, "哟西", contentIntent); NotificationCompat.BigTextStyle textStyle = new NotificationCompat.BigTextStyle(noBuil); textStyle.setBuilder(noBuil); textStyle.bigText("秋天来临了天空像一块覆盖大地的蓝宝石。村外那个小池塘睁着碧澄澄的眼睛,凝望着这美好的天色。一对小白鹅侧着脑袋欣赏自己映在水里的影子"); textStyle.setBigContentTitle("我是BigTextStyle"); textStyle.setSummaryText("我是我是BigTextStyle的setSummaryText"); notificationManager.notify(5, noBuil.build());
2. BigPictureStyle
其存在的一些方法,结合代码与效果图比较明显
setBigContentTitle: 标题
bigPicture: 一个Bitmap
setSummaryText: 最下面的文字信息
1
2
3
4
5
6
7
8
9
10
11Intent launchIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launchIntent, 0); NotificationCompat.Builder noBuil = new NotificationCompat.Builder(this); noBuil.setSmallIcon(R.drawable.feedback_person); noBuil.setContentTitle("ContentTitle"); noBuil.setContentText("This is a ContentText"); NotificationCompat.BigPictureStyle noBigPicStyle = new NotificationCompat.BigPictureStyle(noBuil); noBigPicStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.link, null)); noBigPicStyle.setBigContentTitle("i am big setBigContentTitle"); noBigPicStyle.setSummaryText("i am big setSummaryText"); notificationManager.notify(5, noBuil.build());
3. InboxStyle
其存在的一些方法,结合代码与效果图比较明显
setBigContentTitle: 标题
addLine: 添加一行文字 不同手机显示的高度不一样
setSummaryText: 最下面的文字信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21Intent launchIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launchIntent, 0); NotificationCompat.Builder noBuil = new NotificationCompat.Builder(this); noBuil.setSmallIcon(R.drawable.feedback_person); noBuil.setContentTitle("ContentTitle"); noBuil.setContentText("This is a ContentText"); NotificationCompat.InboxStyle noInbox = new NotificationCompat.InboxStyle(noBuil); noInbox.setBigContentTitle("i am big setBigContentTitle"); noInbox.setSummaryText("i am big setSummaryText"); noInbox.addLine("Line1"); noInbox.addLine("Line2"); noInbox.addLine("Line3"); noInbox.addLine("Line4"); noInbox.addLine("Line5"); noInbox.addLine("Line6"); noInbox.addLine("Line7"); noInbox.addLine("Line8"); noInbox.addLine("Line9"); noInbox.addLine("Line0"); notificationManager.notify(5, noBuil.build());
二、 锁屏界面通知的显示
用户可以通过设置来选择是否允许在锁屏显示敏感Notification内容。
当Notification呈现在锁屏上时,你的app可以控制可见详情权限,调用android.app.Notification.Builder.setVisibility() 来控制可见权限,其中设置值包括:
VISIBILITY_PRIVATE,显示基本信息,例如Notification图标,隐藏Notification全部内容
VISIBILITY_PUBLIC,显示Notification全部内容
VISIBILITY_SECRET,不显示任何内容,包括Notification图标
当设置VISIBILITY_PRIVATE时,你可以提供已经编辑过的隐藏个人详情的Notification,例如,一个SMS app可能呈现一条Notification显示“你有3条新的短信”,但是隐藏短信内容和发送者。呈现这种替代方案的Notification,首先使用Notification.Builder创建更换Notification,当你创建私人的Notification对象,使用Notification.Builder.setPublicVersion()方法附加到它上面的更换通知
这边只能当做一个理论知识,不一定有效
1. 实现点亮屏幕
1
2
3
4
5
6
7
8
9
10PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE); boolean screenOn = pm.isScreenOn(); if (!screenOn) { // 获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag PowerManager.WakeLock wl = pm.newWakeLock( PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright"); wl.acquire(10000); // 点亮屏幕 wl.release(); // 释放 }
2. 实现点击Notification 进入Activity
需对Notification 添加PendingIntent ,并且添加Flags。 同样,有的机型不行,所以Notification适配很麻烦。
1
2
3
4getWindow().addFlags( WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1
2<uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

三、悬挂视图
悬挂视图是android 5.0之后出现的,资料显示写下面一句即可,但本地试验还没成功,不知道是不是适配问题,还在查找
1builder.setFullScreenIntent(hangPendingIntent, true);
最后
以上就是欣喜苗条最近收集整理的关于Notification 二 宽视图、锁屏视图、悬挂样式一、宽视图二、 锁屏界面通知的显示 三、悬挂视图的全部内容,更多相关Notification内容请搜索靠谱客的其他文章。
发表评论 取消回复