Notification PendingIntent

来源:互联网 发布:网络上的时时彩合法吗 编辑:程序博客网 时间:2024/05/18 12:05

1.通知的基本用法

    // 1.创建 NotificationManager对通知进行管理                        // getSystemService(NOTIFICATION_SERVICE)来获取系统的通知服务                        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);                        // 2.为了更好的兼容 使用 support.v4.app.NotificationCompat 来构建                        // Notification                        Notification notification = new NotificationCompat.Builder(                                mContext)                                .setContentTitle("this is a title")                                .setContentText("context")                                // setWhen 显示通知被创建的时间,毫秒为单位,下拉时显示在通知上                                .setWhen(System.currentTimeMillis())                                // SmallIcon使用純alpha 图层的图片进行设计,小图标会显示在 系统状态栏上                                .setSmallIcon(R.drawable.ic_launcher)                                .setLargeIcon(                                        BitmapFactory.decodeResource(                                                getResources(),                                                R.drawable.ic_launcher))                                .build();                        // 3.显示NotificationManager的notify()显示通知,第一个参数是id,要确保每一个通知都不同                        // 第二个参数是 notification 对象                        manager.notify(1, notification);

2.PendingIntent

PendingIntent概述

  1. 含义
    一个Intent 将在某个特定的时刻发生。

  2. 与Intent的区别
    Intent 立即发生。

  3. 典型的使用场景
    给RemoveView添加单击事件,因为RemoteView运行在远程进程中无法通过setOnClick方法来设置单击事件。
    给RemoteView添加单击事件,就必须使用PendingIntent的send和cancel方法来发送和取消特定的待定的Intent。

  4. 支持的3种特定的意图:

    • PendingIntent.getActivity(context, requestCode, intent, flags);
      相当于 startActivity(intents);

    • PendingIntent.getService(context, requestCode, intent, flags);
      相当于 startService(service)

    • PendingIntent.getBroadcast(context, requestCode, intent, flags)
      相当于 sendBroadcast(intent)

第2个requestCode和第4个参数flags: PendingIntent发送的请求码,多数情况下为0即可,requestCode会影响到flags的效果。

  • PendingIntent的匹配规则:什么情况下两个PendingIntent是相同的。

如果两个PendingIntent它们内部的Intent相同并且requestCode也相同,这两个PendingIntent就是相同的。

  • Intent的匹配规则:如果Intent的ComponentName和intent-filter都相同,那么这两个Intent是相同的。

注意:只要Intent的ComponentName和intent-filter都相同,即使Extras不同,那么这两个Intent也是相同。

  • flags常见类型有

  • PendingIntent.FLAG_ONE_SHOT

当前PendingIntent只能被调用一次,然后它会自动cancel,如果还有相同的PendingIntent,那么send方法就会调用失败。对于通知栏消息来说,此类通知只能使用一次,后续的通知单击后无法打开。

  • PendingIntent.FLAG_NO_CREATE

当前PendingIntent不会主动创建,如果当前PendingIntent之前不存在,那么getActivity(),getService(),getBroadcast()方法会直接返回null,既获取PendingIntent失败。不常用。

  • PendingIntent.FLAG_UPDATE_CURRENT

如果PendingIntent如果已经存在,那么他们都会被更新,既它们的Intent中的Extras会被替换成的最新的。

  • PendingIntent.FLAG_CANCEL_CURRENT

如果PendingIntent如果已经存在,那么他们都会被cancel,然后系统会创建一个新的PendingIntent。

结合通知栏消息再描述一遍。分析下边的几种情况,代码如下:

manager.notify(1,notification)

  • 如果notifi的第一个参数id是常量,那么多次调用notify只能弹出一个通知,后续的通知会把前面的通知全部替换掉。
  • 如果notifi的id每次都不同,那么当PendingIntent不匹配时(匹配指Intent相同并且requestCode也相同)在这种
    情况下不管采取何种标志位,这些通知之间都不会相互干扰。

如果PendingIntent处于匹配状态

  • 采用:FLAG_ONE_SHOT

后续通知中的PendingIntent会和第一条通知保持完全一致,包括其中的Extras,单击一条通知后,剩下的通知均无法再打开,当所有的通知被清除后,会再次重复这个过程。

  • 采用:FLAG_CANCEL_CURRENT

只有最新的通知可以打开,之前弹出的通知均无法打开。

  • 采用:FLAG_UPDATE_CURRENT

那么之前弹出的通知中的PendingIntent会被更新,最终它们和最新的一条通知保持完全一致,包括其中的Extras,并且这些通知都是可以打开的。

3.通知的相应事件

将将要启动的Intent对象传入到PendingIntent的getIntent的getActivity()方法里得到PendingIntent的实例,接着调用setContentIntent(paIntent)。

                Intent intent = new Intent(MainActivity.this,                                NotiActivity.class);                        PendingIntent paIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);                        // 1.创建 NotificationManager对通知进行管理                        // getSystemService(NOTIFICATION_SERVICE)来获取系统的通知服务                        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);                        // 2.为了更好的兼容 使用 support.v4.app.NotificationCompat 来构建                        // Notification                        Notification notification = new NotificationCompat.Builder(                                mContext)                                .setContentTitle("this is a title")                                .setContentText("context")                                // setWhen 显示通知被创建的时间,毫秒为单位,下拉时显示在通知上                                .setWhen(System.currentTimeMillis())                                // SmallIcon使用純alpha 图层的图片进行设计,小图标会显示在 系统状态栏上                                .setSmallIcon(R.drawable.ic_launcher)                                .setLargeIcon(                                        BitmapFactory.decodeResource(                                                getResources(),                                                R.drawable.ic_launcher))//取消通知                          .setAutoCancel(true)                                .setContentIntent(paIntent)                                .build();                        // 3.显示NotificationManager的notify()显示通知,第一个参数是id,要确保每一个通知都不同                        // 第二个参数是 notification 对象                        manager.notify(1, notification);                    }

取消通知两种方法:
a.setAutoCancel(true) 传入true,表示点击这个通知时会自动取消掉。
b.NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(1);创建通知时的id

4.通知的进阶技巧

a.设置声音

Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);builder.setSound(uri);

b.设置震动
setVibrate是一个数组,设置手机震动和静止的时间,单位毫秒。含义:静止时常、震动时长、静止时常、震动时长。

//Vibration//        builder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });

别忘了声明震动的权限

    <uses-permission android:name="android.permission.VIBRATE" />

c.LED
3个参数:第一个参数:LED的颜色,第二个参数:LED亮起的时间单位毫秒,第三个参数LED灯暗区的时间,一闪的效果:

 builder.setLights(Color.RED, 3000, 3000);

5.通知的高级功能

a.长文字全部显示出来

.setStyle(                                        new NotificationCompat.BigTextStyle()                                                .bigText("安全通知功能(option_private 按钮),对于此模块,创建了一个基础通知,计划将其作用从包含往来账户余额的用户银行发出的警告。如果用户使设备保持安全,这就是我们应该保护的敏感信息。Android 框架的默认行为将整个通知隐藏在已经编辑视图的后面,如前面的视图。但是此次采取了更友善的方式。第一个用户选项被选中是,我们构造第二个 Notification 实例并将其传递给 setPublicVersion()。这样,在设备解锁时安全地显示此消息。为解锁时则显示的第二个 Notification 中设置的消息。效果如下:"))

b.设置大图

setStyle(                                        new NotificationCompat.BigPictureStyle()                                                .bigPicture(BitmapFactory                                                        .decodeResource(                                                                getResources(),                                                                R.id.icon)))

c.设置优先级
优先级会改变通知显示的大小、顺序
setPriority(NotificationCompat.PRIORITY_MAX)
NotificationCompat.PRIORITY_MAX 最高程度
NotificationCompat.PRIORITY_HIGH 较高 放大 排在比较靠前的位置
NotificationCompat.PRIORITY_MIN 排在更重要的之后

0 0
原创粉丝点击