`

PendingIntent和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.      

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();  


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

转自:http://www.cnblogs.com/lhxin/archive/2011/10/20/2218522.html

分享到:
评论

相关推荐

    Intent和PendingIntent的区别

    简单的总结了Intent和PendtingIntent的区别,经常与alermanger 和notificationmanager一起使用。

    安卓之 (解决问题)PendingIntent和Intent的区别1

    这样我们就可以画出如下示意图:PendingIntent 只能通过下列的静态方法获取//获取Broadcast关联的PendingIntent PendingI

    Android中pendingIntent与Intent的深入分析

    要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, int),getBroadcast(Context, int, Intent, int),getService(Context, int, Intent, int) 分别对应着Intent的3个行为,跳转...

    Notification的用法和PendingIntent使用

    Notification的用法和PendingIntent的使用

    谈谈安卓的Intent注入.pdf

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

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

    Android Proximity Alert 手机接近进警告或提示... PendingIntent pi = PendingIntent.getBroadcast(this, -1, intent, 0);  //添加接近警告  lm.addProximityAlert(latitude, longitude, radius, -1, pi);

    PendingIntent 使用示例

    PendingIntent 分别实现了Notification和sms短信发送的两个示例

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

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

    google_AIP 方法解释

    double latitude, double longitude, float radius, long expiration, PendingIntent intent) 添加一个趋近警告 getAllProviders() 获得所有的LocationProvider列表 getBestProvider(Criteria criteria, ...

    switchactivities:从 PendingIntent 发送时,演示 Intent.FLAG_ACTIVITY_REORDER_TO_FRONT 的小项目不起作用

    通过 PendingIntent 切换活动 这是一个小项目,用于演示通过正常调用 startActivity 在活动之间切换工作正常,但是当我在 PendingIntent 中发送带有该标志的意图然后调用mPendingIntent.send()该标志似乎是忽略:-(。...

    android 后台发短信

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

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

    PendingIntent mPI = PendingIntent.getBroadcast(EX05_03.this, 0, new Intent(), 0); smsManager.sendTextMessage(strDestAddress, null, strMessage, mPI, null); } catch(Exception e) { e....

    详解intent

    当一个application 中有多个 Activity的时候,那么每个Activity之前的传值就是一...1.实体类 Intent 、Context 、 BroadcastReceiver 2.方法类 PendingIntent Context.sendBroadcast 传递的方法 也就是方法类中的两种

    Android系统设置闹钟

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

    Android复杂闹钟

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

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

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

    BroadcastDemo

    PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); // 开始时间 long firstime = SystemClock.elapsedRealtime(); // 60秒一个周期,不停的发送广播 // am....

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

      PendingIntent pi = PendingIntent.getBroadcast(Sample_12_7.this, 0, intent, 0); //创建PendingIntent   am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);//设置闹钟   Toast.makeText(Sample...

    LmqJPushProject

    极光推送(自定义消息)

Global site tag (gtag.js) - Google Analytics