Notifycation(通知)的使用

来源:互联网 发布:pmp网络班价格 编辑:程序博客网 时间:2024/05/17 15:01

特点及作用:

状态栏位置下拉后的内容,用于显示通知列表.

使用例子:当有未读的微信或者短信时,可以在通知栏中看到提醒
播放器播放音乐时,会在通知栏上显示并控制音乐播放

学习目标:向通知栏中添加新的通知信息

使用方式:

大体使用套路:

  1. 初始化NotifycationManager对象,稍后用于发送通知信息
  2. 封装通知信息中要显示的具体内容
  3. 通过NotifycationManager对象调用notify方法发送第2步中封装好的Notifycation对象

普通Notifycation的显示方式:
这里写图片描述

  1. 标题
  2. 大图标
  3. 文本信息
  4. Info信息,即显示在小图标左边的文字信息
  5. 小图标
  6. 发送时间

通知信息的分类 :

  1. 普通通知
  2. 带进度条的通知
  3. 显示超大图片的通知
  4. 显示多行文本的通知
  5. 完全自定义显示内容的通知

NotifycationManager对象的初始化 :

//初始化manager对象        manager = (NotificationManager)this.getSystemService(Service.NOTIFICATION_SERVICE);

(1)普通通知以及点击事件的处理:

//发送普通Notifycation            //封装PendingIntent对象            /*             * 通过getActivity方法获取PedingIntent对象,代表稍候点击后要执行的操作为打开页面的操作             * 1. Context             * 2. 请求码,给本次封装的pi对象设置一个唯一标识             * 3. Intent对象,用于描述具体的跳转动作             * 4. flag标识,用于设置当多次创建请求码相同的pi对象时的创建特点(如是否重复创建等)             * PendingIntent.FLAG_UPDATE_CURRENT             * PendingIntent.FLAG_NO_CREATE             * PendingIntent.FLAG_ONE_SHOT             * PendingIntent.FLAG_CANCEL_CURRENT             */            PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(MainActivity.this,TwoActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);            //封装Notifycation对象            NotificationCompat.Builder nb = new NotificationCompat.Builder(this)            //设置显示内容,注意:设置小图标的方法绝对不可以省略,其他的方法随意            .setContentTitle("通知标题")            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.pic2)) //设置大图标            .setContentText("文本信息内容概要")            .setContentInfo("小左")  //设置info信息            .setSmallIcon(R.drawable.ic_launcher)  //设置小图标            .setWhen(System.currentTimeMillis())   //设置发送时间            .setTicker("提示文本")  //设置滚动提醒的文字            .setOngoing(true)  //设置通知信息是否只能通过代码调用方法移除            /*             * 设置点击事件              * 参数:PendingIntent  延迟意图             */            .setContentIntent(pi)            ;            //通过manager发送            /*             * 参数1: 发送id,作用:             * 当多次发送通知时,用于区分多个通知对象是否是同一个,即在通知列表中是显示多条数据还是一条数据             * 参数2:要发送的通知对象             */            manager.notify(1, nb.build());

通过方法取消通知的显示:

manager.cancel(1);

(2)带进度条的通知:

final NotificationCompat.Builder nb1 = new NotificationCompat.Builder(this)            .setSmallIcon(R.drawable.ic_launcher)            .setContentTitle("带进度条的通知")            /*             * 设置进度条显示效果             * 1. 设置进度条的最大值             * 2. 设置进度条的当前进度值             * 3. 是否模糊显示进度条             */            .setProgress(100, 0, false)            ;            manager.notify(2, nb1.build());            //通过子线程控制进度条中的进度自动增长            new Thread(new Runnable() {                @Override                public void run() {                    // TODO Auto-generated method stub                    while (pro < 100) {                        pro++;                        nb1.setProgress(100, pro, false);                        manager.notify(2, nb1.build());                        try {                            Thread.sleep(100);                        } catch (InterruptedException e) {                            // TODO Auto-generated catch block                            e.printStackTrace();                        }                    }                }            }).start();

(3)显示超大图片的通知

//显示超大图片的通知            //超大图片样式            BigPictureStyle style = new BigPictureStyle();            style.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.desert));            NotificationCompat.Builder nb2 = new NotificationCompat.Builder(this)            .setSmallIcon(R.drawable.ic_launcher)            .setContentTitle("超大图片")            //通过设置样式的方法显示超大图片            .setStyle(style);            manager.notify(3, nb2.build());

(4)显示多行文本的通知

InboxStyle style1 = new InboxStyle();            style1.setBigContentTitle("多行文本标题");            for (int i = 0; i < 5; i++) {                style1.addLine("多行数据多行数据  "+i);            }            NotificationCompat.Builder nb3 = new NotificationCompat.Builder(this)            .setSmallIcon(R.drawable.ic_launcher)            .setStyle(style1);            manager.notify(4, nb3.build());

(5)完全自定义显示内容的通知

//创建用于控制页面显示的RemoteViews对象            //参数1:当前工程的程序包名,2: 要显示的布局文件            RemoteViews rv = new RemoteViews("com.example.day12_1_notifycation", R.layout.custom1);            NotificationCompat.Builder nb4 = new NotificationCompat.Builder(this)            .setSmallIcon(R.drawable.ic_launcher)            /*             * 自定义通知显示内容的方式一:             * 缺陷:自定义内容的高度不能太高,超出指定高度的部分不显示             * 通过setContent方法设置             * 参数为RemoteViews,远程视图,即即使页面退出,也不会对RemoteViews产生影响,依然能够正常显示             *              */            .setContent(rv);            manager.notify(5, nb4.build());

(6)不限制高度自定义显示内容的方式

    //自定义显示内容的方式二:此种不会显示高度            RemoteViews rvs = new RemoteViews("com.example.day12_1_notifycation", R.layout.custom2);            //在代码中控制rvs中控件的显示            rvs.setTextViewText(R.id.textView1, "超高超高超超高");            //单独给自定义布局中的button设置点击事件//          rvs.setOnClickPendingIntent(R.id.button1, pendingIntent)            Notification nbh = new NotificationCompat.Builder(this)                            .setSmallIcon(R.drawable.ic_launcher).build();            nbh.bigContentView = rvs;            manager.notify(6, nbh);
0 0
原创粉丝点击