Intent是一个意图,一个描述了想要启动一个
Activity、Broadcast
或是
Service
的意图。它主要持有的信息是它想要启动的组件(
Activity、Broadcast
或是
Service
)。
PendingIntent 可以看作是对Intent的包装。供当前App之外的其他App调用。有点“被动”或是“Callback”的意思,但不是严格意义上的“ 被动 ”或是“ Callback ”。总之,当前App不能用它马上启动它所包裹的Intent。而是在外部App执行这个PendingIntent时,间接地、实际地调用里面的Intent。PendingIntent主要持有的信息是它所包装的Intent和当前App的Context。正由于PendingIntent中保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行PendingIntent里的Intent,就算在执行时当前App已经不存在了,也能通过存在PendingIntent里的Context照样执行Intent。
intent英文意思是意图,pending表示即将发生或来临的事情。
PendingIntent这个类用于处理即将发生的事情。比如在通知Notification中用于跳转页面,但不是马上跳转。
Intent 是及时启动,intent 随所在的activity 消失而消失。
PendingIntent 可以看作是对intent的包装,通常通过getActivity,getBroadcast ,getService来得到pendingintent的实例,当前activity并不能马上启动它所包含的intent,而是在外部执行 pendingintent时,调用intent的。正由于pendingintent中 保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行pendingintent里的 Intent, 就算在执行时当前App已经不存在了,也能通过存在pendingintent里的Context照样执行Intent。另外还可以处理intent执行 后的操作。常和alermanger 和notificationmanager一起使用。
Intent一般是用作Activity、Sercvice、BroadcastReceiver之间传递数据,而Pendingintent,一般用在 Notification上,可以理解为延迟执行的intent,PendingIntent是对Intent一个包装。
java代码:
1. GSM网络中android发送短信示例
java代码:
PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情 (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就相当于PendingIntent代表了Intent)。本例中别的程序就是发送短信的程序,短信发送成功后要把intent广播出 去 。
函数SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)中参数解释:
1)PendingIntent sentIntent: 当短信发出时,成功的话sendIntent会把其内部的描述的intent广播出去,否则产生错误代码并通过 android.app.PendingIntent.OnFinished进行回调,这个参数最好不为空,否则会存在资源浪费的潜在问题;
2)PendingIntent deliveryIntent: 是当消息已经传递给收信人后所进行的PendingIntent广播。
PendingIntent 可以看作是对Intent的包装。供当前App之外的其他App调用。有点“被动”或是“Callback”的意思,但不是严格意义上的“ 被动 ”或是“ Callback ”。总之,当前App不能用它马上启动它所包裹的Intent。而是在外部App执行这个PendingIntent时,间接地、实际地调用里面的Intent。PendingIntent主要持有的信息是它所包装的Intent和当前App的Context。正由于PendingIntent中保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行PendingIntent里的Intent,就算在执行时当前App已经不存在了,也能通过存在PendingIntent里的Context照样执行Intent。
intent英文意思是意图,pending表示即将发生或来临的事情。
PendingIntent这个类用于处理即将发生的事情。比如在通知Notification中用于跳转页面,但不是马上跳转。
Intent 是及时启动,intent 随所在的activity 消失而消失。
PendingIntent 可以看作是对intent的包装,通常通过getActivity,getBroadcast ,getService来得到pendingintent的实例,当前activity并不能马上启动它所包含的intent,而是在外部执行 pendingintent时,调用intent的。正由于pendingintent中 保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行pendingintent里的 Intent, 就算在执行时当前App已经不存在了,也能通过存在pendingintent里的Context照样执行Intent。另外还可以处理intent执行 后的操作。常和alermanger 和notificationmanager一起使用。
Intent一般是用作Activity、Sercvice、BroadcastReceiver之间传递数据,而Pendingintent,一般用在 Notification上,可以理解为延迟执行的intent,PendingIntent是对Intent一个包装。
java代码:
1. GSM网络中android发送短信示例
java代码:
PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情 (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就相当于PendingIntent代表了Intent)。本例中别的程序就是发送短信的程序,短信发送成功后要把intent广播出 去 。
函数SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)中参数解释:
1)PendingIntent sentIntent: 当短信发出时,成功的话sendIntent会把其内部的描述的intent广播出去,否则产生错误代码并通过 android.app.PendingIntent.OnFinished进行回调,这个参数最好不为空,否则会存在资源浪费的潜在问题;
2)PendingIntent deliveryIntent: 是当消息已经传递给收信人后所进行的PendingIntent广播。
查看PendingIntent 类可以看到许多的Send函数,就是PendingIntent在进行被赋予的相关的操作。
Intent比较简单,类似消息,发送给别的activity,别的activity会立即执行 我主要说说什么是PendingIntent以及它的执行过程 以alarm service为例: 1. activity请求一个alarm一般这样来做: # //创建一个PendingIntent # Intent intent = new Intent(ALARM_ALERT_ACTION); # intent.putExtra(ID, id); # intent.putExtra(TIME, atTimeInMillis); # PendingIntent sender = PendingIntent.getBroadcast( # context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); # //获得AlarmMnager并注册一个新闹铃, # //一次性闹铃的设置 # # AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); # am.set(AlarmManager.POWER_OFF_WAKEUP, atTimeInMillis, sender); 2. AlarmManager.set调用AlarmManagerService.set 3. AlarmManagerService.set的核心代码如下: 我们可以看到它就是把PendingIntent保存起来而已 4. alarm manager service会定时查看是否有alarm到期了,如果到期了做相应处理。 5. AlarmThread会调用 alarm.operation.send(mContext, 0, mBackgroundIntent.putExtra( Intent.EXTRA_ALARM_COUNT, alarm.count), mResultReceiver, mHandler); 也就是通过PendingIntent.send执行intent操作,alarm这个就会发送ALARM_ALERT_ACTION的broadcast。 补充说明: 1. PendingIntent重要特点是异步处理。 2. 另外有一个要说明的是PendingIntent.onFinished,它可以作为PendingIntent.send的一个参数, 我们知道PendingIntent.send一般是在service中执行,这个调用的send后,回调onFished类的onSendFinished,所以onSendFinished一般也是在service中执行的 |