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