您的位置:首页 > 移动开发 > Android开发

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例子:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: