Intent和PendingIntent的区别

来源:互联网 发布:mac excel怎么加 编辑:程序博客网 时间:2024/05/29 15:47

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(this0new Intent(this,Activity2.class), 0));//即将跳转页面,还没跳转  //PendingIntent.getActivity(this0new Intent(this,Activity2.class) 获取 PendingIntent对象
  11.         
  12. NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);   
  13.         manager.notify(0,notice);   //使用NotificationManager的时候 再去调用PendingIntent对象进行跳转
  14.     }  


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. //new Intent(...)  e.g: PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, new ////Intent("com.homer.pendingintent.pendingbroadcast"), 0);
  7. sms.sendTextMessage(number, null, msg, pi, null);   
  8. Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();  

完整例子,可以参考: http://download.csdn.net/detail/sunboy_2050/4917350#comment 

代码解释 
      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://developer.android.com/reference/android/app/PendingIntent.html

public final class
PendingIntent
extends Object
implements Parcelable
简介
PendingIntent用于描述Intent及其最终的行为. 
你可以通过getActivity(Context context, int requestCode, Intent intent, int flags)系列方法从系统取得一个用于启动一个Activity的PendingIntent对象,
可以通过getService(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个用于启动一个Service的PendingIntent对象
可以通过getBroadcast(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个用于向BroadcastReceiver的Intent广播的PendingIntent对象
返回的PendingIntent可以递交给别的应用程序,然后继续处理。这里的话你可以稍后才处理PendingIntent中描述的Intent及其最终行为。
当你把PendingIntent递交给别的程序进行处理时,PendingIntent仍然拥有PendingIntent原程序所拥有的权限(with the same permissions and identity).当你从系统取得一个PendingIntent时,一定要非常小心才行。比如,通常,如果Intent目的地是你自己的component(Activity/Service/BroadcastReceiver)的话,你最好采用在Intent中显示指定目的component名字的方式,以确保Intent最终能发到目的,否则Intent最后可能不知道发到哪里了。一个PendingIntent就是Android系统中的一个token(节点,这个应该是Linux或C\C++用语)的一个对象引用,它描述了一些将用于retrieve的数据(这里,这些数据描述了Intent及其最终的行为)。
这就意味着即使PendingIntent原进程结束了的话, PendingIntent本身仍然还存在,可在其他进程(PendingIntent被递交到的其他程序)中继续使用.如果我在从系统中提取一个PendingIntent的,而系统中有一个和你描述的PendingIntent对等的PendingInent, 那么系统会直接返回和该PendingIntent其实是同一token的PendingIntent,而不是一个新的tokenPendingIntent。然而你在从提取PendingIntent时,通过FLAG_CANCEL_CURRENT参数,让这个老PendingIntent的先cancel()掉,这样得到的pendingInten和其token的就是新的了。
通过FLAG_UPDATE_CURRENT参数的话,可以让新的Intent会更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。另外,我们也可以在PendingIntent的原进程中调用PendingIntent的cancel ()把其从系统中移除掉。

注意:两个PendingIntent对等是指它们的operation一样, 且其它们的Intent的action, data, categories, components和flags都一样但是它们的Intent的Extra可以不一样
主要常量
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。
主要成员函数
getActivities系列方法
系列方法将从系统取得一个用于启动一个Activity的PendingIntent对象。

public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)

Since: API Level 1

Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent). Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.

因为对于Context的startActivity方法,如果不是在其子类(Activity)中调用,那么必须对Intent加上FLAG_ACTIVITY_NEW_TASK。

具体可以参照Context中对startActivity方法的说明或Activity和Task的基本模型
Parameters
contextThe Context in which this PendingIntent should start the activity.requestCodePrivate request code for the sender (currently not used).intentIntent of the activity to be launched.flagsMay be FLAG_ONE_SHOTFLAG_NO_CREATEFLAG_CANCEL_CURRENTFLAG_UPDATE_CURRENT, or any of the flags as supported byIntent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.
当我们使用Intent.fillIn()方法时,表示其Intent的某个数据项可以被send方法的Inent参数进行覆盖重写。
Returns
  Returns an existing or new PendingIntent matching the given parameters. May return null only if FLAG_NO_CREATE has been supplied.

public static PendingIntent getActivities (Context context, int requestCode, Intent[] intents, int flags)

Since: API Level 11

Like getActivity(Context, int, Intent, int), but allows an array of Intents to be supplied. The first Intent in the array is taken as the primary key for the PendingIntent, like the single Intent given to getActivity(Context, int, Intent, int). Upon sending the resulting PendingIntent, all of the Intents are started in the same way as they would be by passing them to startActivities(Intent[]).

The first intent in the array will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent. (Activities after the first in the array are started in the context of the previous activity in the array, so FLAG_ACTIVITY_NEW_TASK is not needed nor desired for them.)

The last intent in the array represents the key for the PendingIntent. In other words, it is the significant element for matching (as done with the single intent given togetActivity(Context, int, Intent, int), its content will be the subject of replacement by send(Context, int, Intent) andFLAG_UPDATE_CURRENT, etc. This is because it is the most specific of the supplied intents, and the UI the user actually sees when the intents are started.

Parameters
contextThe Context in which this PendingIntent should start the activity.requestCodePrivate request code for the sender (currently not used).intentsArray of Intents of the activities to be launched.flagsMay be FLAG_ONE_SHOTFLAG_NO_CREATEFLAG_CANCEL_CURRENTFLAG_UPDATE_CURRENT, or any of the flags as supported byIntent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.
当我们使用Inent.fillIn()所支持的flags时,表示其Intent的数据项可以被send方法的Intent参数覆盖重写。
Returns
  • Returns an existing or new PendingIntent matching the given parameters. May return null only if FLAG_NO_CREATE has been supplied.
getService方法
该方法将系统取得一个用于启动一个Service的PendingIntent对象.

public static PendingIntent getService (Context context, int requestCode, Intent intent, int flags)

Since: API Level 1

Retrieve a PendingIntent that will start a service, like calling Context.startService(). The start arguments given to the service will come from the extras of the Intent.

Parameters
contextThe Context in which this PendingIntent should start the service.requestCodePrivate request code for the sender (currently not used).intentAn Intent describing the service to be started.flagsMay be FLAG_ONE_SHOTFLAG_NO_CREATEFLAG_CANCEL_CURRENTFLAG_UPDATE_CURRENT, or any of the flags as supported byIntent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.
当我们使用Intent.fillIn()方法的flags时,它表示其Intent的数据项可以被send方法的Intent参数覆盖重写。
Returns
  • Returns an existing or new PendingIntent matching the given parameters. May return null only if FLAG_NO_CREATE has been supplied.
getBroadcast方法
该方法将从系统取得一个用于向BroadcastReceiver的Intent广播的PendingIntent对象

public static PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags)

Since: API Level 1

Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast().

Parameters
contextThe Context in which this PendingIntent should perform the broadcast.requestCodePrivate request code for the sender (currently not used).intentThe Intent to be broadcast.flagsMay be FLAG_ONE_SHOTFLAG_NO_CREATEFLAG_CANCEL_CURRENTFLAG_UPDATE_CURRENT, or any of the flags as supported byIntent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.
当我们使用Intent.fillIn()方法的flags时,它表示其Intent的数据项可以被send方法的Intent参数覆盖重写。
Returns
  • Returns an existing or new PendingIntent matching the given parameters. May return null only if FLAG_NO_CREATE has been supplied.
send系列方法
该系列主要用于触发PendingIntent的Intent行为。用其Intent或启动一个Activity,或启动一个Service,或向BroadcastReceiver发送Intent广播。

public void send ()

Since: API Level 1

Perform the operation associated with this PendingIntent.

Throws
PendingIntent.CanceledExceptionThrows CanceledException if the PendingIntent is no longer allowing more intents to be sent through it.

public void send (Context context, int code, Intent intent)

Since: API Level 1

Perform the operation associated with this PendingIntent, allowing the caller to specify information about the Intent to use.

Parameters
contextThe Context of the caller.
该参数是因为intent参数才需要提供的,所用如果你的intent参数不为null的话,该参数也不能为null.codeResult code to supply back to the PendingIntent's target.intentAdditional Intent data. See Intent.fillIn() for information on how this is applied to the original Intent.
Throws
PendingIntent.CanceledExceptionThrows CanceledException if the PendingIntent is no longer allowing more intents to be sent through it.

public void send (int code, PendingIntent.OnFinished onFinished, Handler handler)

Since: API Level 1

Perform the operation associated with this PendingIntent, allowing the caller to be notified when the send has completed.

Parameters
codeResult code to supply back to the PendingIntent's target.onFinishedThe object to call back on when the send has completed, or null for no callback.
通过该参数,我们可以设置在Intent发送成功后的回调函数。handlerHandler identifying the thread on which the callback should happen. If null, the callback will happen from the thread pool of the process.
用于说明onFinished参数指定的回调函数,最终在哪个Handler中进行调用。
Throws
PendingIntent.CanceledExceptionThrows CanceledException if the PendingIntent is no longer allowing more intents to be sent through it.

public void send (Context context, int code, Intent intent, PendingIntent.OnFinished onFinished, Handler handler)

Since: API Level 1

Perform the operation associated with this PendingIntent, allowing the caller to specify information about the Intent to use and be notified when the send has completed.

For the intent parameter, a PendingIntent often has restrictions on which fields can be supplied here, based on how the PendingIntent was retrieved ingetActivity(Context, int, Intent, int)getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).

Parameters
contextThe Context of the caller. This may be null if intent is also null.
该参数是因为intent参数才需要提供的,一般是当前的context,如果你的intent参数不为null话,该函数也不能为null.codeResult code to supply back to the PendingIntent's target.intentAdditional Intent data. See Intent.fillIn() for information on how this is applied to the original Intent. Use null to not modify the original Intent.onFinishedThe object to call back on when the send has completed, or null for no callback.
通过该参数,我们可以指定Intent发送成功后的回调函数。handlerHandler identifying the thread on which the callback should happen. If null, the callback will happen from the thread pool of the process.
该参数说明onFinished参数指定的回调函数将在哪个Handler中进行调用。
Throws
PendingIntent.CanceledExceptionThrows CanceledException if the PendingIntent is no longer allowing more intents to be sent through it.
cancel()函数

public void cancel ()

Since: API Level 1

Cancel a currently active PendingIntent. Only the original application owning an PendingIntent can cancel it.

只有PengdingIntent的原应用程序才能调用cancel()来把它从系统中移除掉。

getTargetPackage()函数

public String getTargetPackage ()

Since: API Level 1

Return the package name of the application that created this PendingIntent, that is the identity under which you will actually be sending the Intent. The returned string is supplied by the system, so that an application can not spoof its package.

Returns
  • The package name of the PendingIntent, or null if there is none associated with it.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

pendingIntent简单理解 

pendingIntent字面意义:等待的,未决定的Intent。
要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, int)getBroadcast(Context, int, Intent, int)getService(Context, int, Intent, int)  分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。
参数有4个,比较重要的事第三个和第一个,其次是第四个和第二个。可以看到,要得到这个对象,必须传入一个Intent作为参数,必须有context作为参数。
pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。pendingIntent执行的操作实质上是参数传进来的Intent的操作,但是使用pendingIntent的目的在于它所包含的Intent的操作的执行是需要满足某些条件的。
主要的使用的地方和例子:通知Notificatio的发送,短消息SmsManager的发送和警报器AlarmManager的执行等等。

Android的状态栏通知(Notification)

如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息。

步骤:

获取通知管理器NotificationManager,它也是一个系统服务

建立通知Notification notification = new Notification(icon, null, when);

为新通知设置参数(比如声音,震动,灯光闪烁)

把新通知添加到通知管理器

发送消息的代码如下:

//获取通知管理器

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)

int icon = android.R.drawable.stat_notify_chat;

long when = System.currentTimeMillis();//通知发生的时间为系统当前时间

//新建一个通知,指定其图标和标题

Notification notification = new Notification(icon, null, when);//第一个参数为图标,第二个参数为短暂提示标题,第三个为通知时间

notification.defaults = Notification.DEFAULT_SOUND;//发出默认声音

notification.flags |= Notification.FLAG_AUTO_CANCEL;//点击通知后自动清除通知

Intent openintent = new Intent(this, OtherActivity.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openintent, 0);//当点击消息时就会向系统发送openintent意图

notification.setLatestEventInfo(this, “标题”, “我是内容", contentIntent);

mNotificationManager.notify(0, notification);//第一个参数为自定义的通知唯一标识

 

重点是setLatestEventInfo( )方法的最后一个参数!!!!它是一个PendingIntent!!!!!!!!!

这里使用到了PendingIntent(pend本意是待定,不确定的意思)

PendingIntent可以看作是对Intent的包装。PendingIntent主要持有的信息是它所包装的Intent和当前ApplicationContext。正由于PendingIntent中保存有当前ApplicationContext,使它赋予带他程序一种执行的Intent的能力,就算在执行时当前Application已经不存在了,也能通过存在PendingIntent里的Context照样执行Intent

 

PendingIntent的一个很好的例子:

SmsManager的用于发送短信的方法:

sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);

第一个参数:destinationAddress 对方手机号码

第二个参数:scAddress 短信中心号码 一般设置为空

第三个参数:text 短信内容

第四个参数:sentIntent判断短信是否发送成功,如果你没有SIM卡,或者网络中断,则可以通过这个itent来判断。注意强调的是“发送”的动作是否成功。那么至于对于对方是否收到,另当别论

第五个参数:deliveryIntent当短信发送到收件人时,会收到这个deliveryIntent。即强调了“发送”后的结果

就是说是在"短信发送成功""对方收到此短信"才会激活 sentIntentdeliveryIntent这两个Intent。这也相当于是延迟执行了Intent


上面两个例子可以理解,PendingIntent就是一个可以在满足一定条件下执行的Intent,它相比于Intent的优势在于自己携带有Context对象,这样他就不必依赖于某个activity才可以存在。

1 0
原创粉丝点击