Android开发之PendingIntent的使用

 原链接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例子:

复制代码
 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

猜你喜欢

转载自blog.csdn.net/qq_30295213/article/details/78271063