PendingIntent

PendingIntent它的直译是:待处理意图,这样翻译,大家就猜出它的作用是什么了,用于处理一些定义但是不立即使用的意图,最常见的就是用户点击通知,然后跳转指定的页面:

最常用的方法估计就下面这个了:

public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)

类似的还有:

  • PendingIntent.getActivity(context, 0, notifyIntent, 0);
  • PendingIntent.getService(context, 0, notifyIntent, 0);
  • PendingIntent.getBroadcast(context, 0, notifyIntent, 0);

第一个参数是上下文,第二个参数 requestCode,第三个参数是 Intent,第四个参数是对参数的操作标识

第一、三个没什么好说的,重点介绍另外两个:

第二个参数:requestCode:

Private request code for the sender
当同时有多个通知的时候,可通过这个参数来互相区别

第四个参数:flags

FLAG_CANCEL_CURRENT:
Flag indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one.
如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。

FLAG_UPDATE_CURRENT:
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.
如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。

FLAG_NO_CREATE:
Flag indicating that if the described PendingIntent does not already exist, then simply return null instead of creating it.
如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null。

FLAG_ONE_SHOT:
Flag indicating that this PendingIntent can be used only once.
该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。

FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT的区别在于能否新new一个Intent,FLAG_CANCEL_CURRENT能够新new一个Intent,而FLAG_UPDATE_CURRENT则不能。

Notification

显示一则通知,旧的Notification有不少方法是已经过时的,下面是新的:

          public void create(Context context){
Notification notification = new Notification.Builder(context)
//设置通知标题
.setContentTitle("标题")
//设置通知栏显示内容
.setContentText("测试内容")
//设置通知栏点击时的意图,就比如上面的PendingIntent
.setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL))
//通知首次出现在通知栏,带上升动画效果的
.setTicker("测试通知")
//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
.setWhen(System.currentTimeMillis())
//设置该通知优先级
.setPriority(Notification.PRIORITY_DEFAULT)
//设置这个标志当用户单击面板就可以让通知将自动取消
.setAutoCancel(true)
//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,
//用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,
//主动网络连接)
.setOngoing(false)
//向通知添加声音、闪灯和振动效果的最简单、
//最一致的方式是使用当前的用户默认设置
.setDefaults(Notification.DEFAULT_VIBRATE)
//设置通知小ICON
.setSmallIcon(R.mipmap.ic_launcher)
.build();

PendingIntent、Notification常用方法的更多相关文章

  1. Android开发4: Notification编程基础、Broadcast的使用及其静态注册、动态注册方式

    前言 啦啦啦~(博主每次开篇都要卖个萌,大家是不是都厌倦了呢~) 本篇博文希望帮助大家掌握 Broadcast 编程基础,实现动态注册 Broadcast 和静态注册 Broadcast 的方式以及学 ...

  2. Android新旧版本Notification

    Android新旧版本Notification 在notification.setLatestEventInfo() 过时了 以前: NotificationManager mn = (Notific ...

  3. Notification

    一:普通Notification 1.内容标题setContentTitle(...) 2.大图标setLargeIcon(Bitmap) 3.内容setContentText(...) 4.内容附加 ...

  4. [置顶] android之Notification版本兼容性问题

    首先先来创建一个notification提示 //概要 String tickerText = context.getResources().getText(R.string.app_name).to ...

  5. 同时显示多个 Notification

    主要出在PendingIntent.getActivity();的第二个参数,API文档里虽然说是未被使用的参数(给出的例子也直接写0的),实际上是通过该参数来区别不同的Intent的,如果id相同, ...

  6. Android学习总结(十五) ———— Notification(状态栏通知)基本用法

    一.Notification基本概念  Notification是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容.我们在用手机的时候 ...

  7. Android Notification 版本适配方案

    Notification 介绍见:https://developer.android.com/reference/android/app/Notification.html Android api 一 ...

  8. Androidn Notification的使用,解决找不到setLatestEventInfo方法

    转自:http://blog.csdn.net/songyachao/article/details/51245370#comments 今天使用4.0.3使用 Notification notifi ...

  9. android的几种“通知”方式简单实现(Notification&NotificationManager)

    关于通知Notification相信大家都不陌生了,平时上QQ的时候有消息来了或者有收到了短信,手机顶部就会显示有新消息什么的,就类似这种.今天就稍微记录下几种Notification的用法.3.0以 ...

随机推荐

  1. LINUX 笔记-文本过滤

    ^                        只匹配行首 $                       只匹配行尾 *                        一个单字符后紧跟*,匹配0个 ...

  2. [ACdream]瑶瑶带你玩激光坦克

    题目链接:http://acdream.info/contest?cid=1269#problem-B Problem Description 有一款名为激光坦克的游戏,游戏规则是用一个坦克发出激光来 ...

  3. LAMP 实现全过程及wordpress的搭建

    一.介绍 1. LAM(M)P: L:linux A:apache (httpd) M:mysql, mariadb M:memcached 缓存 P:php, perl, python WEB 资源 ...

  4. swift之函数式编程(三)

    文章来源于<Functional Programing in Swift>,本系列仅仅是观后概括的一些内容 Wrapping Core Image 上一篇文章我们介绍了 高阶函数并且展示了 ...

  5. virualbox 搭建 otter

    前言 为了学习otter,上一篇我们讲到了 otter 必要软件的安装,参考:virualbox 安装 otter 必备软件,现在安装otter,相比官方文档,我们尽量简化安装步骤. virualbo ...

  6. Hello Kiki(中国剩余定理——不互质的情况)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  7. Python面向对象篇之元类,附Django Model核心原理

    关于元类,我写过一篇,如果你只是了解元类,看下面这一篇就足够了. Python面向对象之类的方法和属性 本篇是深度解剖,如果你觉得元类用不到,呵呵,那是因为你不了解Django. 在Python中有一 ...

  8. vue-修改vue项目运行端口号

    一.导语 最近在研究,左侧是导航,右侧是显示对应的内容,左右可单独滚动,不互相影响,如何实现? 萝卜蹲的游戏大家都玩过,白萝卜蹲,白萝卜蹲,白萝卜蹲完红萝卜蹲,可是若是在含有滚动条的页面的情况下,白萝 ...

  9. PHP小技巧

    1.js获取服务器年月日 var date= '<?php echo date("Y-m-d",time())?>';//获取服务器年月

  10. 【译】Asp.Net Identity Cookies 格式化-中英对照版

    原文出处 Trailmax Tech Max Vasilyev: ASP.Net MVC development in Aberdeen, Scotland I've been reached out ...