`

Android Intent和PendingIntent的区别详细分析

 
阅读更多

刚才一个例子中用到了PendingIntent,与之前学过的Intent有些类似,所以百度了一下结合自己的理解做个整理:

       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. private void showNotify(){
  2. Notification notice=new Notification();
  3. notice.icon=R.drawable.icon;
  4. notice.tickerText=”您有一条新的信息”;
  5. notice.defaults=Notification.DEFAULT_SOUND;
  6. notice.when=10L;
  7. // 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒
  8. //notice.vibrate = new long[] { 100, 250, 100, 500 };出错?
  9. //notice.setLatestEventInfo(this, “通知”, “开会啦”, PendingIntent.getActivity(this, 0, null, 0));
  10. notice.setLatestEventInfo(this, “通知”, “开会啦”, PendingIntent.getActivity(this, 0, new Intent(this,Activity2.class), 0));//即将跳转页面,还没跳转
  11. NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);
  12. manager.notify(0,notice);
  13. }
复制代码


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

java代码:

  1. String msg =”你好,美女”;
  2. String number = “135****6784″;
  3. SmsManager sms = SmsManager.getDefault(); 

  4. PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(…),0);
  5. sms.sendTextMessage(number, null, msg, pi, null);
  6. Toast.makeText(SmsActivity.this,”发送成功”,Toast.LENGHT_LONG).show();
复制代码


       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在进行被赋予的相关的操作。

 

分享到:
评论

相关推荐

    Android中pendingIntent与Intent的深入分析

    主要介绍了Android中pendingIntent的深入分析的相关资料,需要的朋友可以参考下

    Android编程实现PendingIntent控制多个闹钟的方法

    要用 android.app.PendingIntent.getBroadcast(Context context, int requestCode, Intent intent)来实现控制多个闹钟,关键点在于其中的一个参数requestCode. 举例说明如下: public void setClock(){ if(lva....

    自制发送短信程序,android,SmsManager与PendingIntent

    import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; /*必需引用telephony.gsm.SmsManager类才能使用sendTextMessage()*/ import android.telephony.SmsManager; ...

    谈谈安卓的Intent注入.pdf

    归纳了Intent入 的4种形式:Intent转换与复制、 Action/Component/Data注 、PendingIntent误用与 parseUri注 入 归纳了利用自动化的工具具发现这4类形式的方法,通过批 量的扫描,可以轻易发现这些漏洞 在每种都找到了...

    Android Intent实现页面跳转的两种方法

    本文实例为大家分享了Intent实现页面跳转的两种的方法,供大家参考,具体内容如下 下图中两个不同的方法就是两种页面之间跳转的情况 1).跳转不返回数据 2).跳转返回数据 实例: 第一种启动方式(跳转不返回数据) ...

    Android 使用TimePickerDialog设置闹钟的例子.rar

    Android 使用TimePickerDialog设置闹钟的例子,dialog=new TimePickerDialog(//创建TimePickerDialog对象  this,  new TimePickerDialog.OnTimeSetListener(){ //创建OnTimeSetListener监听器   @Override   ...

    Android系统设置闹钟

    pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, intent, 0); calendar=Calendar.getInstance(); alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE); } public void...

    android 后台发短信

    Intent itSend = new Intent(SMS_SEND_ACTIOIN); Intent itDeliver = new Intent(SMS_DELIVERED_ACTION); PendingIntent mSendPI = PendingIntent.getBroadcast(context, 0, itSend, 0); ...

    简单实现Android闹钟功能

    import android.app.PendingIntent; import android.app.TimePickerDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.S

    Android复杂闹钟

    另一个是利用Intent传递广播的Intent的时候,传参数的时候,记得设置PendingIntent sender=PendingIntent.getBroadcast(context,0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);的PendingIntent.FLAG_UPDATE_...

    精通ANDROID 3(中文版)1/2

    1.5.6 Android媒体和电话组件  1.5.7 Android Java包  1.6 利用Android源代码  1.7 本书的示例项目  1.8 小结  第2章 设置开发环境  2.1 设置环境  2.1.1 下载JDK 6  2.1.2 下载Eclipse 3.6  ...

    Android Proximity Alert 手机接近进警告或提示功能一例.rar

    Android Proximity Alert 手机接近进警告或提示功能一例  LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);  //定义接近区域的大致经度、纬度  double longitude = 113.39; ...

    Android中AlarmManager+Notification实现定时通知提醒功能

    AlarmManager实质是一个全局的定时器,是Android中常用的一种系统级别的提示服务,在指定时间或周期性启动其它组件(包括Activity,Service,BroadcastReceiver)。本文将讲解一下如何使用AlarmManager实现定时提醒...

    Android 创建备忘录及闹铃提醒的例子.rar

    Android 创建备忘录的例子,可自定义备忘录内容,自定义时间和自定义提醒闹钟的类型、铃声等。相关代码:  c.setTimeInMillis(System.currentTimeMillis());//设置当前时间  c.set(Calendar.HOUR_OF_DAY, ...

    Android 在状态栏添加Notification信息图标及提示.rar

    这个例子演示Android 在状态栏添加Notification信息图标及提示,相信大家对这个功能已经不陌生了,手机中安装的APP,一般都会在后台运行,时不时会在手机顶部的状态栏中显示应用的图标,滑出状态栏会看到详细的信息...

    Android输入手机号发送短信示例.rar

    Android输入手机号发送短信示例,EditText number框中的是电话号码,EditText body框中的是短信内容:  public void onCreate(Bundle savedInstanceState) {//重写的onCreate方法  super.onCreate...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1...

    精通Android 3 (中文版)2/2

    1.5.6 Android媒体和电话组件  1.5.7 Android Java包  1.6 利用Android源代码  1.7 本书的示例项目  1.8 小结  第2章 设置开发环境  2.1 设置环境  2.1.1 下载JDK 6  2.1.2 下载Eclipse 3.6  ...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1...

Global site tag (gtag.js) - Google Analytics