Android开发之PendingIntent的使用
2015-07-18 14:15
761 查看
PendingIntent,待确定的意图,等待的意图
官网链接:http://developer.android.com/reference/android/app/PendingIntent.html
官网关于该类的继承关系,PendingIntent继承自Object。因为该类为final,所以没有子类,无法被继承。
要想得到一个PendindIntent对象,需要使用方法类的静态方法 ,%20int)]getActivity(Context, int, Intent, int),,%20int)]getActivities(Context, int, Intent[], int),getBroadcast(Context, int, Intent, int),getService(Context, int, Intent, int)
这几个静态方法分别对应着Intent的行为,跳转到Activity,Activities,Broadcast,Service。
这4个静态方法的参数都一样,其中第一个和第三个参数比较的重要,其次是第二个和第四个。第一个参数传入当前的context,第三个参数传入intent.
PendingIntent是一个特殊的Intent,主要区别是intent是立马执行,PendingIntent是待确定的Intent。PendingIntent的操作实际上是传入的intent的操作。
使用pendingIntent的目的主要是用于所包含的intent执行是否满足某些条件。
主要的地方:Notification,SmsManager,AlarmManager等
1.Notification例子:
2.SmsManager例子:
主要常量
FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。
FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null。
FLAG_ONE_SHOT:该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。
FLAG_UPDATE_CURRENT:如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。
请参考:http://blog.csdn.net/hudashi/article/details/7060837
官网链接:http://developer.android.com/reference/android/app/PendingIntent.html
官网关于该类的继承关系,PendingIntent继承自Object。因为该类为final,所以没有子类,无法被继承。
要想得到一个PendindIntent对象,需要使用方法类的静态方法 ,%20int)]getActivity(Context, int, Intent, int),,%20int)]getActivities(Context, int, Intent[], int),getBroadcast(Context, int, Intent, int),getService(Context, int, Intent, int)
这几个静态方法分别对应着Intent的行为,跳转到Activity,Activities,Broadcast,Service。
这4个静态方法的参数都一样,其中第一个和第三个参数比较的重要,其次是第二个和第四个。第一个参数传入当前的context,第三个参数传入intent.
PendingIntent是一个特殊的Intent,主要区别是intent是立马执行,PendingIntent是待确定的Intent。PendingIntent的操作实际上是传入的intent的操作。
使用pendingIntent的目的主要是用于所包含的intent执行是否满足某些条件。
主要的地方:Notification,SmsManager,AlarmManager等
1.Notification例子:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) mBuilder.setSmallIcon(R.drawable.notification_icon) mBuilder.setContentTitle("My notification") mBuilder.setContentText("Hello World!"); Intent resultIntent = new Intent(this, ResultActivity.class); PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); int mNotificationId = 001; // Gets an instance of the NotificationManager service NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // Builds the notification and issues it. mNotifyMgr.notify(mNotificationId, mBuilder.build());
2.SmsManager例子:
SmsManager smsManage = SmsManager.getDefault(); Intent intent=new Intent("SEND_SMS_ACTION"); PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0); smsManage.sendTextMessage("13xxxxxxxxx", null, "这是一条短信", pendingIntent, null);
主要常量
FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。
FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null。
FLAG_ONE_SHOT:该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。
FLAG_UPDATE_CURRENT:如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。
请参考:http://blog.csdn.net/hudashi/article/details/7060837
相关文章推荐
- Android_运用log4j打印日志
- Android学习笔记----Activity的生命周期图示
- 教你在Android Studio 中进行单元测试
- Android开发者指南(6) —— AIDL
- 接Android外包
- android gson解析Json,Could not find class 'com.google.gson.Gson'
- Android 长时间运行任务说明
- Android之——常见Bug及其解决方案
- 【转】Android ListView长按事件触发点击事件
- Android--Parcelable接口用法
- Android (shape,gradient)详解
- android 相关下载
- Android多媒体:音效链
- Android多媒体:AudioSystem,AudioService和AudioManager
- 对android volley框架的理解(一)
- Android Material Design之Toolbar与Palette实践
- android菜鸟学习笔记30----Android使用百度地图API(一)准备工作及在应用中显示地图
- Android项目重构之路:实现篇 读后思考
- Android View深入学习(一),View的测量(Measure)过程
- Android的Looper,Handler以及线程间的通信