Android 5.X 新特性详解——Notification

Posted 黄飞_hf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 5.X 新特性详解——Notification相关的知识,希望对你有一定的参考价值。

Google在android 5.0 上又进一步改进了通知栏,优化了Notification。当长按Notification的时候,会显示消息来源。Notification会有一个从白色到灰色的动画切换效果,最终显示发出这个Notification的调用者。同时,在Android 5.X 设备上,锁屏状态下我们也可以看见Notification通知了。

基本Notification

        /**
         * 给点击Notification后要执行的操作增进一个Intent,由于这个Intent不是马上执行的,而是由用户触发的,
         * 所以Android给这样的Intent提供了一个PendingIntent来帮助完成这样的延迟操作。
         */
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
        // 构造PendingIntent
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        // 创建Notification对象
        Notification.Builder builder = new Notification.Builder(this);
        // 设置Notification的各种属性
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setContentTitle("Basic Notifications");
        builder.setContentText("I am a basic notification");
        builder.setSubText("it is really basic");
        // 通过NotificationManager来发出Notification
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //调用notify方法时,需要传进去一个ID。每个Notification都会有一个ID,这个ID就是用来区分不同的APP的Notification的。
        notificationManager.notify(NOTIFICATION_ID_BASIC, builder.build());

折叠式Notification

折叠式Notification也是一种自定义视图的Notification,常常用于显示长文本。它拥有两个视图状态,一个是普通状态下的视图状态,另一个是展开状态下的视图状态。

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sina.com"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));

        //通过RemoteViews来创建自定义的Notification视图
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
        contentView.setTextViewText(R.id.textView, "show me when collapsed");
        RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.notification_expanded);

        Notification notification = builder.build();
        //将一个视图指定为Notification正常状态下的视图
        notification.contentView = contentView;
        //将一个视图指定为Notification展开时的视图
        notification.bigContentView = expandedView;

        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(NOTIFICATION_ID_COLLAPSE, notification);

悬挂式Notification

这种被称为Headsup的Notification方式,可以在屏幕上方产生Notification且不会打断用户的操作,能给用户以Notification形式的通知。

        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setCategory(Notification.CATEGORY_MESSAGE)
                .setContentTitle("Headsup Notification")
                .setContentText("I am a Headsup notification.")
                .setSubText("Heads-Up Notification on Android 5.0");

        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClass(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        //通过setFullScreenIntent将一个Notification变成悬挂式Notification
        builder.setFullScreenIntent(pendingIntent, true);

        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(NOTIFICATION_ID_HEADSUP, builder.build());

显示等级的Notification

Android 5.X 将Notification分成了三个等级。

  • VISIBILITY_PRIVATE——表明只有当没有锁屏的时候会显示
  • VISIBILITY_PUBLIC——标明在任何情况下都会显示
  • VISIBILITY_SECRET——表明在pin、password等安全锁和没有锁的情况下才能够显示
        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.visibility_radio_group);
        Notification.Builder builder = new Notification.Builder(this)
                .setContentTitle("Notification for Visibility Test")
                //设置Notification显示的位置
                .setCategory(Notification.CATEGORY_EMAIL)
                //设置Notification背景颜色
                .setColor(Color.RED);
        switch (radioGroup.getCheckedRadioButtonId()) 
            case R.id.radio_button_public:
                //设置Notification的显示等级
                builder.setVisibility(Notification.VISIBILITY_PUBLIC);
                builder.setContentText("Public");
                builder.setSmallIcon(R.mipmap.ic_public);
                break;
            case R.id.radio_button_private:
                builder.setVisibility(Notification.VISIBILITY_PRIVATE);
                builder.setContentText("Private");
                builder.setSmallIcon(R.mipmap.ic_private);
                break;
            case R.id.radio_button_secret:
                builder.setVisibility(Notification.VISIBILITY_SECRET);
                builder.setContentText("Secret");
                builder.setSmallIcon(R.mipmap.ic_secret);
                break;
        
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(NOTIFICATION_ID_VISIBILITY, builder.build());

程序运行效果图如下:

代码地址

以上是关于Android 5.X 新特性详解——Notification的主要内容,如果未能解决你的问题,请参考以下文章

Android 5.X 新特性详解——Notification

Android 5.X 新特性详解——Notification

Android 5.X 新特性详解——列表与卡片

Android 5.X 新特性详解——Toolbar

Android 5.X 新特性详解——Activity过渡动画

Android 5.X新特性