【学习笔记】Android5.X Notification新功能初识

  • 基本的Notification

    /**
         * 基本的Notification
         */
        private void notificationBasic(){
            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 Notification");
            builder.setContentText("这是一个基本通知");
            builder.setSubText("副标题");
            //通过NotificationManager来发出Notification
            NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(NOTIFICATION_ID_BASIC, builder.build());
        }



    效果:

    【学习笔记】Android5.X Notification新功能初识_第1张图片

  • 折叠式的Notification

    /**
         * 折叠式的Notification
         */
        private void notificationCollapsed(){
            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));
    
            //通过RemoteViews来创建自定义的Notification视图
            RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
            contentView.setTextViewText(R.id.textView, "show me whe collapsed");
    
            Notification notification = builder.build();
            notification.contentView = contentView;
    
            //通过RemoteViews来创建自定义的Notification视图
            RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.notification_expanded);
            notification.bigContentView = expandedView;
    
            NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            nm.notify(NOTIFICATION_ID_COLLAPSE, notification);
        }



    【学习笔记】Android5.X Notification新功能初识_第2张图片

    【学习笔记】Android5.X Notification新功能初识_第3张图片

  • 悬挂式的Notification

    /**
         * 悬挂式的Notification
         */
        private void notificationHeadSup(){
            Notification.Builder builder = new Notification.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setPriority(Notification.PRIORITY_DEFAULT)
                    .setCategory(Notification.CATEGORY_MESSAGE)
                    .setContentTitle("悬挂式通知")
                    .setContentText("这是一个悬挂式通知");
    
            Intent push = new Intent();
            push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            push.setClass(this, NotificationActivity.class);
            PendingIntent pi = PendingIntent.getActivity(this, 0, push, PendingIntent.FLAG_CANCEL_CURRENT);
            builder.setContentText("android5.0悬挂式通知")
                    .setFullScreenIntent(pi, true);
    
            NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            nm.notify(NOTIFICATION_ID_HEADSUP, builder.build());
        }



    【学习笔记】Android5.X Notification新功能初识_第4张图片

  • 显示等级的Notification

    /**
         * 显示等级的Notification
         * Android 5.X将Notification分成了三个等级
         * VISIBILITY_PRIVATE 表示只有当没有锁屏的时候会显示
         * VISIBILITY_PUBLIC 表示在任何情况下都会显示
         * VISIBILITY_SECRET 表明在pin、password等安全锁和没有锁屏的情况下才能够显示
         */
        private void notificationVisibility(){
            Notification.Builder builder = new Notification.Builder(this)
                    .setContentTitle("Notification for Visibility Test")
                    .setColor(getResources().getColor(R.color.colorAccent));
            switch (rgVisibility.getCheckedRadioButtonId()){
                case R.id.rdo_private:
                    builder.setVisibility(Notification.VISIBILITY_PRIVATE);
                    builder.setContentText("Private");
                    builder.setSmallIcon(R.mipmap.ic_private);
                    break;
                case R.id.rdo_public:
                    builder.setVisibility(Notification.VISIBILITY_PUBLIC);
                    builder.setContentText("Public");
                    builder.setSmallIcon(R.mipmap.ic_public);
                    break;
                case R.id.rdo_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());
        }




你可能感兴趣的:(【学习笔记】Android5.X Notification新功能初识)