Android开发之PendingIntent的使用
2017-10-18 11:34
531 查看
原链接http://www.cnblogs.com/liyiran/p/4656821.html
官网关于该类的继承关系,PendingIntent继承自Object。因为该类为final,所以没有子类,无法被继承。
要想得到一个PendindIntent对象,需要使用方法类的静态方法 getActivity(Context,
int, Intent, int),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
官网关于该类的继承关系,PendingIntent继承自Object。因为该类为final,所以没有子类,无法被继承。
要想得到一个PendindIntent对象,需要使用方法类的静态方法 getActivity(Context,
int, Intent, int),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例子:
1 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 2 mBuilder.setSmallIcon(R.drawable.notification_icon) 3 mBuilder.setContentTitle("My notification") 4 mBuilder.setContentText("Hello World!"); 5 Intent resultIntent = new Intent(this, ResultActivity.class); 6 PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT); 7 mBuilder.setContentIntent(resultPendingIntent); 8 int mNotificationId = 001; 9 // Gets an instance of the NotificationManager service 10 NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 11 // Builds the notification and issues it. 12 mNotifyMgr.notify(mNotificationId, mBuilder.build());
2.SmsManager例子:
1 SmsManager smsManage = SmsManager.getDefault(); 2 Intent intent=new Intent("SEND_SMS_ACTION"); 3 PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0); 4 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开发之PendingIntent的使用
- Android 程式开发:(四)使用Intent调用“内置”应用 —— 4.2浅析Intent
- Android开发中IntentService的使用(二)
- 【Android 开发教程】使用Intent-Filter
- Android开发打开文件的Intent及使用
- Android 程式开发:(四)使用Intent调用“内置”应用 —— 4.3配置Intent-Filter
- 关于android中widget的PendingIntent的使用
- android 开发手记 activity 之间数据的传递 intent 对象的使用
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- android之PendingIntent使用
- Android开发之使用Intent进行自定义类型数据传输
- Android 开发笔记—— Intent的使用
- 【Android 开发教程】使用Intent发送短信
- Android开发打开文件的Intent及使用
- android开发基础:Intent与Bundle的使用,实现页面跳转及信息传递
- 【Android 开发教程】使用Intent调用内置应用程序
- 【Android 开发教程】使用IntentService执行异步操作
- Android开发中Intent的使用总结
- PendingIntent android.app.PendingIntent.getActivity的使用
- Android 程式开发:(四)使用Intent调用“内置”应用 —— 4.1范例