PendingIntent的解读。
2016-03-22 17:07
387 查看
前言
最近做一个dialog需要不点击确定过段时间还是会提示。就用到了PendingIntent和AlarmManager的组合来完成这项任务,中间遇到些问题。这里闲了下来,就看了下源码。但是忽然感觉源码不能回答我的问题,因为这里一个非常重要的参数(target)通过native来新构建的(ps这里貌似有adil,这一块不熟悉),调用的进程(AlarmManager、Notifiction)等目标进程不容易跟踪。但是通过读javadoc也了解了不少内容。就在这里写一篇短的博客当一个笔记。PendingIntent的作用
基本功能很像intent。因为target本身一定包含一个intent,有机会再具体研究具体构成。这里主要说明下从javadoc得到的信息。具体作用是在当前context不存在时候,PendingIntent依然可以正常工作,这里非常适合定时工作,例如用在AlarmManager、Notifiction等中。注意事项
作为构造参数的intent尽量使用详细的classname。这里推荐使用java intent.setclass(Context packageContext, Class<?> cls);
这里很多近似的代码。这里都是intent的内容。就不在这里详细研究了。
跨进程注意权限问题。
当有两个同样动作的intent的动作时候,应该让他们两个不一样,具体如何做我也不会(ps我是直接动用系统flag设置强制更新)。
有些方法没研究,
主要方法
这里主要是用来启动activity和service和发送广播。因此包含了三个主要方法getactivity()、getservice()、getBroadcast(),可以创建出一个用于启动activity、service、发送广播里的pengdingintent的实例。这里包含四个参数。第一个是context,也就是启动的context。第二个是requestcode。这里是用来返回值时使用的,具体如何使用没有测试(我都是默认填0,因为我不需要返回)。第三个参数是intent,也就是所要执行的动作(刚才提要尽量使用具体的classname)。第四个是个flag。标志着对于已经存在相同目的的PendingIntent是如何处理。这里简单介绍几个参数的意思,1. FLAG_ONE_SHOT 当前PendingIntent仅仅能使用一次。使用过后立马回收
2.FLAG_NO_CREATE 、
*Flag indicating that if the described PendingIntent does not * already exist, then simply return null instead of creating it.
这里貌似是说如果没有就返回空。不在创建。
3.FLAG_CANCEL_CURRENT要是当前存在就取消当前的。
4.FLAG_UPDATE_CURRENT更新当前的
代码讲解
获得serviceIntent intent = new Intent(this,MyService.class); PendingIntent pendingIntent = PendingIntent.getService(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
获取activity
Intent intent1 = new Intent(); intent1.setClassName(this.getPackageName(), Main2Activity.class.getName()); PendingIntent pendingIntent1 = PendingIntent.getActivity(this,0,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
获取广播
Intent intent2 = new Intent("android.TEST.BRO"); PendingIntent pendingIntent2 = PendingIntent.getService(this,0,intent2,PendingIntent.FLAG_UPDATE_CURRENT);
具体使用这里写了两个例子供大家参考:
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); alarmManager.set (AlarmManager.RTC, Calendar.getInstance().getTimeInMillis() + 1000, pendingIntent);
Notification notify3 = new Notification.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setTicker("通过notifiction启动activity") .setContentTitle("Notification Title") .setContentText("This is the notification message") .setContentIntent(pendingIntent1).setNumber(1).build(); notify3.flags |= Notification.FLAG_AUTO_CANCEL; NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1,notify3);
附上示例代码
下载地址相关文章推荐
- 从源码安装Mysql/Percona 5.5
- 浅析Ruby的源代码布局及其编程风格
- asp.net 抓取网页源码三种实现方法
- JS小游戏之仙剑翻牌源码详解
- JS小游戏之宇宙战机源码详解
- jQuery源码分析之jQuery中的循环技巧详解
- 本人自用的global.js库源码分享
- java中原码、反码与补码的问题分析
- PHP网页游戏学习之Xnova(ogame)源码解读(六)
- C#获取网页HTML源码实例
- PHP网页游戏学习之Xnova(ogame)源码解读(八)
- PHP网页游戏学习之Xnova(ogame)源码解读(四)
- JS小游戏之极速快跑源码详解
- JS小游戏之象棋暗棋源码详解
- android源码探索之定制android关机界面的方法
- 基于Android设计模式之--SDK源码之策略模式的详解
- Android游戏源码分享之2048
- C语言借助EasyX实现的生命游戏源码
- C实现的非阻塞方式命令行端口扫描器源码
- PHP网页游戏学习之Xnova(ogame)源码解读(七)