对于Android的Intent相关内容,可能部分初级Android开发者不知道PendingIntent是干什么的? 对于Notification和SmsManager中的sendMessage以及AlarmManager中的set这些方法中均有 PendingIntent,到底PendingIntent和Intent有哪些不同呢?
一、Intent
通常Android中的Intent位于 android.content.Intent的实现比较简单,直接从Object类实现,内部主要是保存了一些String或Int、轻量级的数组,提供了一些方法主要是赋值或取值。
二、PendingIntent
这里和Intent的不同分在了android.app.PendingIntent这个包中,属于app层而不是数据存储封装的content层,从首段我们看到了PendingIntent是针对将要发生的事情,比如短信发送时,本对象用于跟踪未来短信的接收情况,主要是短信回执报告和发送成功或失败,因为GSM通讯到RIL再到移动基站的过程很漫长,通过开一个Thread等待对于我们的应用是比较麻烦和耗资源,而Android的框架层的TelephonyManager底层远程服务会跟踪,最终通过PendingIntent来跟踪,有关具体实现原理和代码如下:
Java代码:
public final class PendingIntent implements Parcelable {
//实现了Parcelable接口,可以方便的处理二进制数据和用于远程服务的数据交换
private final IIntentSender mTarget;
public static final int FLAG_ONE_SHOT = 1<<30;
public static final int FLAG_NO_CREATE = 1<<29;
public static final int FLAG_CANCEL_CURRENT = 1<<28;
public static final int FLAG_UPDATE_CURRENT = 1<<27;
public static class CanceledException extends AndroidException {
public CanceledException() {
}
public CanceledException(String name) {
super(name);
}
public CanceledException(Exception cause) {
super(cause);
}
}
public interface OnFinished {
void onSendFinished(PendingIntent pendingIntent, Intent intent,int resultCode, String resultData, Bundle resultExtras);
}
private static class FinishedDispatcher extends IIntentReceiver.Stub implements Runnable {
private final PendingIntent mPendingIntent;
private final OnFinished mWho;
private final Handler mHandler;
private Intent mIntent;
private int mResultCode;
private String mResultData;
private Bundle mResultExtras;
FinishedDispatcher(PendingIntent pi, OnFinished who, Handler handler) {
mPendingIntent = pi;
mHandler = handler;
}
public void performReceive(Intent intent, int resultCode,String data, Bundle extras, boolean serialized, boolean sticky) {
mIntent = intent;
mResultCode = resultCode;
mResultData = data;
mResultExtras = extras;
if (mHandler == null) {
run();
} else {
mHandler.post(this);
}
}
public void run() {
mWho.onSendFinished(mPendingIntent, mIntent, mResultCode,mResultData, mResultExtras);
}
}
public static PendingIntent getActivity(Context context, int requestCode,Intent intent, int flags) {
String packageName = context.getPackageName();
String resolvedType = intent != null ? intent.resolveTypeIfNeeded(
context.getContentResolver()) : null;
try {
IIntentSender target =ActivityManagerNative.getDefault().getIntentSender(
IActivityManager.INTENT_SENDER_ACTIVITY, packageName,null, null, requestCode, intent, resolvedType, flags);
return target != null ? new PendingIntent(target) : null;
} catch (RemoteException e) {
}
return null;
}