Android-Intent和PendingIntent的关系

Intent和PendingIntent的关系
Intent和PendingIntent的关系越看越迷惑,花了一段时间分析Android的JavaDoc终于有点心得,在此与大家分享:
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。
比较难理解,希望大家多提意见!

Java代码 复制代码  收藏代码
  1. Notification n = new Notification(R.drawable.face_1, "Service启动", System.currentTimeMillis());   
  2. PendingIntent contentIntent = PendingIntent.getActivity(this0new Intent(this, TServiceHolder.class), 0);   
  3. n.setLatestEventInfo(this"任务标题""任务内容", contentIntent);   
  4. nManager.notify(NOTIFICATION_ID, n); // 任务栏启动  
Notification n = new Notification(R.drawable.face_1, "Service启动", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TServiceHolder.class), 0);
n.setLatestEventInfo(this, "任务标题", "任务内容", contentIntent);
nManager.notify(NOTIFICATION_ID, n); // 任务栏启动

PendingIntent和Intent的区别:An Intent is something that is used right now; a PendingIntent is something that may create an Intent in the future. You will use a PendingIntent with Notifications, AlarmManager, etc.

1. GSM网络中android发送短信示例

(1)代码节选
Java代码 复制代码  收藏代码
  1. String msg ="你好,美女";   
  2. String number = "135****6784";   
  3. SmsManager sms = SmsManager.getDefault();   
  4.   
  5. PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0);   
  6. sms.sendTextMessage(number, null, msg, pi, null);   
  7. Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();  
String msg ="你好,美女";
String number = "135****6784";
SmsManager sms = SmsManager.getDefault();

PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0);
sms.sendTextMessage(number, null, msg, pi, null);
Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();

(2)代码解释

      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在进行被赋予的相关的操作。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值