Android Notification的各种Style, 一句链式调用就搞定。

特别强调:所有的Style在miui上几乎都失效,这是miui都原因
国内的应用,普遍来说,都不太注意Notification的样式,只是提供了一个快捷的信息展示而已,而除了使用常规样式,Messaging, BigText, BigPicture, Inbox, Media这几种样式,根本就不去用,甚至不知道。
出现这种现象,原因是多方面的,比如说需求不需要,简单展示信息就够了,或者单纯是程序员本身懒,还有Notification以及其Compat库体系复杂,不知道怎么用(对于Notification的support库,有v4与v7两个,呈现效果不同,容易造成困扰)。
不能因为各种原因,就放弃了Notification的良好体验。针对这种情况,我封装了一套Notification的便捷库。
废话不多说,直接上Github:NotificationStyle.
该库已经可以通过jcenter引用来使用了。

compile 'com.github.boybeak:notify:1.3.0'

该库中,核心的类就是NotificationCenter,使用方法如下:

NotificationCenter.with(this)
                .smallIcon(R.mipmap.ic_launcher)
                .contentText("BigText ContentText")
                .contentTitle("BigText ContentTitle")
                .ticker("BigText Ticker")
                .when(System.currentTimeMillis(), true)
                .withPendingIntent()
                .flags(0)
                .requestCode(100)
                .activityContent(new Intent(this, MainActivity.class))
                .asBigTextStyle()
                .bigContentTitle(BigTextData.BIG_TEXT_TITLE)
                .summaryText(BigTextData.BIG_TEXT_SUMMARY)
                .bigText(BigTextData.BIG_TEXT)
                .show(4);

通过这样的链式调用,你就可以显示一个BigText style的Notification了。其重点在于asBigTextStyle方法,在这个方法后,链式设置其对应的属性就可以了,比如bigContentTitle, summaryText等。
该框架也支持有限的自定义的Style,比如说对BigPicture Style进行自定义,由于多数情况下,图片需要异步获取,这就需要我们先拿到图片,再设置样式。在Github上的Demo里,有一个异步Style范例。代码如下:

public class AsyncBigPicture extends BigPicture {

    private static final String TAG = AsyncBigPicture.class.getSimpleName();

    private String url;

    public AsyncBigPicture remoteBigPicture (String url) {
        this.url = url;
        return this;
    }

    @Override
    public AsyncBigPicture summaryText(CharSequence summaryText) {
        super.summaryText(summaryText);
        return this;
    }

    @Override
    public BigPicture summaryText(@StringRes int summaryText) {
        super.summaryText(summaryText);
        return this;
    }

    public void showWhenPictureReady (final int id) {
        Glide.with(getCenter().getContext()).load(url)
                .asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                Log.v(TAG, "showWhenPictureReady onResourceReady");
                bigPicture(resource).bigLargeIcon(resource).show(id);
            }

            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                super.onLoadFailed(e, errorDrawable);
                getCenter().show(id);
                Log.v(TAG, "showWhenPictureReady onLoadFailed " + e.getMessage());
            }
        });
    }

    public void showWhenPictureReady (final String tag, final int id) {
        Glide.with(getCenter().getContext()).load(url)
                .asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                bigPicture(resource).bigLargeIcon(resource).show(tag, id);
            }

            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                show(tag, id);
            }
        });
    }
}

在这段代码中,使用了Glide作为异步加载图片的来源。该异步类需要采用如下方式引用:

NotificationCenter.with(this)
                .smallIcon(R.mipmap.ic_launcher)
                .contentText("customStyle ContentText")
                .contentTitle("customStyle ContentTitle")
                .ticker("customStyle Ticker")
                .when(System.currentTimeMillis(), true)
                .asStyle(new AsyncBigPicture())
         .remoteBigPicture("http://files.17173.com/forum/fz_tele-01/images/2013/09/21/184655g3ggu3u7rgxx7g35.jpg")
                .showWhenPictureReady(5);

重点在于asStyle方法,通过该方法,可以使用一个自定义的Style。

其显示效果如下:
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值