android Notification分析—— 您可能会遇到各种问题
使用的各种总结上线通知,csdn还有一个非常到位的总结,不这样做,反复总结,学生需要能够搜索自己或参考下面给出的链接。
研究开始时仔细阅读一些,今天,功能开发,一些问题和经验自己最近的遭遇给大家分享。
一、非常难逃避de兼容问题
1、直接new Notification()这样的方式已经过时,因此自己也没有去细究这样的方式,直接使用的是new NotificationCompat.Builder(context).build()(这个在support v4包中,以下的内容全是以这样的方式来实现的)
2、自己定义布局的兼容
使用NotificationCampat来做自己定义布局我们能够这样做:
RemoteViews rvMain = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
//TODO rvMain...
NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setContent(rvMain);
// TOOD ...
在4.0以上。这样是没问题,可是在2.3的时候,你会发现这样根本无效,因此我们须要换一种方式:
RemoteViews rvMain = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
//TODO rmMain...
NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setContent(rvMain);
// TOOD ...
Notification notification = builder.build();
if(Build.VERSION.SDK_INT <= 10){notification.contentView = rvMain;
}
3、通知栏上的操作事件
先说说我们能够处理的通知事件:
setContentIntent():用户点击通知时触发
setFullScreenIntent()://TODO 这个在通知显示的时候会被调用
setDeleteIntent():用户清除通知时触发,能够是点击清除button,也能够是左右滑动删除(当然了,前提是高版本号)
2.3及下面是无法处理自己定义布局中的操作事件的,这样我们就不要去考虑添加自己定义button了。
二、可能会遇到的问题
1、Ticker图标显示仅仅有中间一部分
new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_notice_small, 0)
我们能够通过这种方式来设置Ticker时显示的图标。在我们測试的时候小米和sony的机器上出现过,解决的方法是把之前的大图标(我们最初是72×72)替换成如今的32×32的小图标。并把它从之前的xxhdpi移到hdpi目录中。
2、Ticker不显示:直接显示通知的图标,中间的Ticker过程没有显示
这个没有找到是什么原因。直接把setFullScreenIntent(mPIFullScreen, false)这行注销就可以
3、点击通知,通知列表不消失
这个仅仅在华为的一个机器上遇到过。解决的方法是在点击事件之后发一个关闭的广播:
sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
參考:http://stackoverflow.com/questions/18261969/clicking-android-notification-actions-does-not-close-notification-drawer
4、出现各种乱
这里说的混乱是指通知没有显示出来、多条通知点击触发的事件同样、多条通知的Intent同样等等。这个时候你可能须要去检查通知的ID和PendingIntent的requestCode。
5、各种机型的适配
这个是相当混乱的。比方大多数是黑底白字,可是部分华为机型是灰底黑字。有些不支持设置自己定义布局中的字体颜色;有些会屏蔽自己定义布局的背景颜色和透明度(比方vivo部分机型),有些甚至你仅仅能自己定义通知栏的部分布局(左面的图标是改不了的,华为的某款):
以前为了追求自己定义布局的左側图标和默认通知的左側图标宽度同样,大小相仿。做了非常多适配,比方各种dimens,可是不过适配,还是不能支持全部机型,遂放弃了。到眼下为止没找到一个完美的解决方式,市面上非常多优秀的应用做的也不是非常好。对照下左側图标宽度你就知道。近期在墨迹天气中发现这个功能:
这算不算对混乱的一种妥协呢?
三、自己定义的一些扩展
从開始处理通知中的操作事件,到实现一个比现有通知更高的通知。我们的欲望在不断的添加。
尽管美好的东西总是来得晚一些。可是眼下来看。这些想法我们都能够实现了。
4.1中默认的三种big style能够满足我们非常多时候的需求,比方:
从左到右依次应该为BigTextStyle、BigPictureStyle、InboxStyle,这个就不多说了,照着demo就能弄出来。值得注意的是,默认展开BigStyle是用两手指头平行下滑,折叠是上滑(魅族的比較奇葩。不是这样操作的)。以下是百度音乐播放器的通知效果,在此简单实现下。效果如图:
知道怎么做了,事实上是非常easy实现的。但关键的是不知道怎么做。
首先看到这个效果,自己去尝试了各种方法。比方指定视图的高度,在不同的机器(主要是高版本号的)机器上执行,发现都不行。google一下。没有结果。怎么办?反编译吧,在资源文件里发现了layout-v16目录下有个布局,这个布局就是我们上图的布局,在一大堆java文件里找到这样一行代码:
notification.bigContentView = *****;
怎么样?明确了吧~~就是关键句,查看api,这个属性须要16及以上才支持。因此我们在实现的时候能够这样写:
if(Build.VERSION.SDK_INT >= 16){notification.bigContentView = rvMain;
}
将我们自己的RemoteView付给bigContentView。真正实现的时候你可能会发现,这个高度不是我们随意指定的。是的,确是如此。它的高度是100dp。这样我们就能够通过自己定义RemoteView来实现各种绚丽的通知了。
四、通知NotificationManager的源代码
本想在分析下NotificationManager,可是发现他的代码非常少,仅仅要完毕自己的Notification然后调用它的方法就能够。
详细的实现就是底层的事情了,又到软肋的
只是简单浏览还是发现了一点点——和Toast的关联,他们都使用了INotificationManager:
static private INotificationManager getService() {if (sService != null) {return sService;}sService = INotificationManager.Stub.asInterface(ServiceManager.getService("notification"));return sService;}
Toast的show():
/*** Show the view for the specified duration.*/public void show() {if (mNextView == null) {throw new RuntimeException("setView must have been called");}INotificationManager service = getService();String pkg = mContext.getPackageName();TN tn = mTN;tn.mNextView = mNextView;try {service.enqueueToast(pkg, tn, mDuration);} catch (RemoteException e) {// Empty}}
NotificatioManager的notify():
/*** Post a notification to be shown in the status bar. If a notification with* the same tag and id has already been posted by your application and has not yet been* canceled, it will be replaced by the updated information.** @param tag A string identifier for this notification. May be {@code null}.* @param id An identifier for this notification. The pair (tag, id) must be unique* within your application.* @param notification A {@link Notification} object describing what to* show the user. Must not be null.*/public void notify(String tag, int id, Notification notification){int[] idOut = new int[1];INotificationManager service = getService();String pkg = mContext.getPackageName();if (notification.sound != null) {notification.sound = notification.sound.getCanonicalUri();if (StrictMode.vmFileUriExposureEnabled()) {notification.sound.checkFileUriExposed("Notification.sound");}}if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");try {service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,notification, idOut, UserHandle.myUserId());if (id != idOut[0]) {Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);}} catch (RemoteException e) {}}
參考(引用):
http://blog.csdn.net/vipzjyno1/article/details/25248021
http://blog.csdn.net/feng88724/article/details/6259071
demo:下载
版权声明:本文博主原创文章,博客,未经同意不得转载。
转载于:https://www.cnblogs.com/gcczhongduan/p/4842168.html
android Notification分析—— 您可能会遇到各种问题相关推荐
- android notification 的总结分析,Android Notification的多种用法总结
android notification的多种用法总结 我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今 ...
- Android JNI入门第五篇——Android.mk分析
转载请标明出处: http://blog.csdn.net/michael1112/article/details/56671708 江东橘子的博客 Android.mk文件是在使用NDK编译C代码时 ...
- Android Notification总结
Android Notification总结 目录[-] 一.通知的主要功能 二.通知简介 三.通知的使用流程 四.使用NotificationCompat.Builder设置通知的属性: 五.管 ...
- Android 逆向分析大全
转载:Android 逆向分析大全:https://www.jianshu.com/p/a12d04fc748f 1. 概述 1.1 分析步骤 通用逆向分析步骤 1. 了解该模块正向编程相关方法 2. ...
- Android系统性能优化(54)---Android性能分析专题
Android性能分析专题 1. 背景:Android App优化, 要怎么做? 2. Android App优化之性能分析工具 3. Android App优化之提升你的App启动速度之理论基 ...
- android菜单更改,Android修改分析:删除设置中菜单
Android修改分析:删除设置中菜单 设置中的菜单主要分为两种添加方式: `` 1.静态加载 所谓自主添加,就是通过xml文件,自主通过添加布局到Fragment.Activity等加载到设置菜单中 ...
- Android手机开发总结——Android核心分析
导读:对于Android开发者来说,成系列的技术文章对他们的技术成长帮助最大.如下是我们向您强烈推荐的主题为Android开发的第一个系列文章. <Android核心分析>整理如下: 1. ...
- Android源代码分析之类方法与组件名词解释(持续更新)
Android源代码分析方法组件详解 1. 类方法 1.1 Handler方法 1.2 Looper方法 1.3 Binder 1.4 Instrumentation 2. 组件 2.1 AMS.WM ...
- Android核心分析
导读:对于Android开发者来说,成系列的技术文章对他们的技术成长帮助最大.如下是我们向您强烈推荐的主题为Android开发的第一个系列文章. <Android核心分析>整理如下: 1. ...
最新文章
- 对代码生成器的一点想法
- java怎么导入项目?java已有项目如何导入eclipse?
- android UI进阶之实现listview的分页加载
- a5d2 phy驱动
- 怎么把好几行弄成一行_我在git merge的时候遇到了冲突,怎么解决?
- PHP+jQuery实现翻板抽奖
- python des加密文件_python DES3 加密解密
- jquery筛选方法-选取父子元素
- 【OpenCV】“帧差法”实现移动物体的检测(车辆识别)
- [EE261学习笔记] 4.常用的几个傅里叶变换相关公式
- PHP中一个好玩的性别判断扩展
- Unity一键更换TextMeshPro的字体
- vscode react 代码格式化
- 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶(n为正整数)总共有多少种跳法。
- JMeter发送数据到Kafka
- 简单测试服务器磁盘读写速度
- PTA天天练(大于身高的平均值 )
- 主机配置文件(HostProfiles)的创建与应用
- 申宝证券-个股分化指数窄幅整理
- Prometheus Operator 通过additional 添加target