需求是這樣的,一個 Android 應用要求彈出一個通知,同時通知上還有兩個按鈕,通知本身點擊和按鈕點擊都對應不同的響應,但是在實現后發現不管點哪個按鈕最后都響應同一個事件, 既返回的參數都是同一個。
我的實現方式是這樣的,BroadcastReceiver 接收到通知后根據 key 作出不同的響應:
val pendingIntent1 = PendingIntent.getBroadcast(context, 0, Intent(BROADCAST_ACTION).apply {
setClass(context, BroadcastReceiver::class.java)
putExtra(key, value1)
}, PendingIntent.FLAG_UPDATE_CURRENT)
val pendingIntent2 = PendingIntent.getBroadcast(context, 0, Intent(BROADCAST_ACTION).apply {
setClass(context, BroadcastReceiver::class.java)
putExtra(key, value2)
}, PendingIntent.FLAG_UPDATE_CURRENT)
val pendingIntent3 = PendingIntent.getBroadcast(context, 0, Intent(BROADCAST_ACTION).apply {
setClass(context, BroadcastReceiver::class.java)
putExtra(key, value3)
}, PendingIntent.FLAG_UPDATE_CURRENT)
一開始每個響應PendingIntent都是全局變量,只實例化一次,我懷疑是因為后續更新通知導致,所以改成每次更改通知時都新建 一個 PendingIntent,結果還是不行。
后來我把每個 PendingIntent 打印出來如下:
PendingIntent{2fd4457: android.os.BinderProxy@2f2cb44}
PendingIntent{5e7742d: android.os.BinderProxy@2f2cb44}
PendingIntent{c05d62: android.os.BinderProxy@2f2cb44}
而PendingIntent對象都toString() 方法是這樣的:
@Override
public String toString() {
StringBuilder sb = new StringBuilder(128);
sb.append("PendingIntent{");
sb.append(Integer.toHexString(System.identityHashCode(this)));
sb.append(": ");
sb.append(mTarget != null ? mTarget.asBinder() : null);
sb.append('}');
return sb.toString();
}
從上面兩張圖可以得出結論,雖然 PendingIntent 每次都是一個新的對象,但是他們都指向一個 IIntentSender,吶泥,why?
首先, 我們看看 PendingIntent 是如何創建的?
PendingIntent 提供幾個靜態方法來實例化自己:
PendingIntent.getActivity()
PendingIntent.getBroadcast()
PendingIntent.getService()
...
以 getBroadcast() 為例
public static PendingIntent getBroadcast(Context context, int requestCode,
Intent intent, @Flags int flags) {
return getBroadcastAsUser(context, requestCode, intent, flags, context.getUser());
}
/**
* @hide
* Note that UserHandle.CURRENT will be interpreted at the time the
* broadcast is sent, not when the pending intent is created.
*/
public static PendingIntent getBroadcastAsUser(Context context, int requestCode,
Intent intent, int flags, UserHandle userHandle) {
String packageName = context.getPackageName();
String resolvedType = intent != null ? intent.resolveTypeIfNeeded(
context.getContentResolver()) : null;
try {
intent.prepareToLeaveProcess(context);
IIntentSender target =
ActivityManager.getService().getIntentSender(
ActivityManager.INTENT_SENDER_BROADCAST, packageName,
null, null, requestCode, new Intent[] { intent },
resolvedType != null ? new String[] { resolvedType } : null,
flags, null, userHandle.getIdentifier());
return target != null ? new PendingIntent(target) : null;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
而上面打印出來的不同的 PendingIntent 對應同一個 target 就是上面通過 ActivityManager.getService().getIntentSender(...) 創建的,這里 ActivityManager.getService 返回的是 ActivityManagerService,我們去看看這個方法:
@Override
public IIntentSender getIntentSender(int type,
String packageName, IBinder token, String resultWho,
int requestCode, Intent[] intents, String[] resolvedTypes,
int flags, Bundle bOptions, int userId) {
//...刪掉部分不重要的代碼
synchronized(this) {
int callingUid = Binder.getCallingUid();
int origUserId = userId;
userId = mUserController.handleIncomingUser(Binder.getCallingPid(), callingUid, userId,
type == ActivityManager.INTENT_SENDER_BROADCAST,
ALLOW_NON_FULL, "getIntentSender", null);
if (origUserId == UserHandle.USER_CURRENT) {
// We don't want to evaluate this until the pending intent is
// actually executed. However, we do want to always do the
// security checking for it above.
userId = UserHandle.USER_CURRENT;
}
try {
if (callingUid != 0 && callingUid != SYSTEM_UID) {
final int uid = AppGlobals.getPackageManager().getPackageUid(packageName,
MATCH_DEBUG_TRIAGED_MISSING, UserHandle.getUserId(callingUid));
if (!UserHandle.isSameApp(callingUid, uid)) {
String msg = "Permission Denial: getIntentSender() from pid="
+ Binder.getCallingPid()
+ ", uid=" + Binder.getCallingUid()
+ ", (need uid=" + uid + ")"
+ " is not allowed to send as package " + packageName;
Slog.w(TAG, msg);
throw new SecurityException(msg);
}
}
// ... 最終會調用和返回這個方法, 繼續往下看看
return getIntentSenderLocked(type, packageName, callingUid, userId,
token, resultWho, requestCode, intents, resolvedTypes, flags, bOptions);
} catch (RemoteException e) {
throw new SecurityException(e);
}
}
}
IIntentSender getIntentSenderLocked(int type, String packageName,
int callingUid, int userId, IBinder token, String resultWho,
int requestCode, Intent[] intents, String[] resolvedTypes, int flags,
Bundle bOptions) {
// 刪掉部分不重要的代碼
final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0;
final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0;
final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0;
flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT
|PendingIntent.FLAG_UPDATE_CURRENT);
// 這里根據傳經來的一些參數構建來一個 key, 這個里很關鍵
PendingIntentRecord.Key key = new PendingIntentRecord.Key(type, packageName, activity,
resultWho, requestCode, intents, resolvedTypes, flags,
SafeActivityOptions.fromBundle(bOptions), userId);
WeakReference<PendingIntentRecord> ref;
ref = mIntentSenderRecords.get(key);
PendingIntentRecord rec = ref != null ? ref.get() : null;
if (rec != null) {
if (!cancelCurrent) {
if (updateCurrent) {
if (rec.key.requestIntent != null) {
rec.key.requestIntent.replaceExtras(intents != null ?
intents[intents.length - 1] : null);
}
if (intents != null) {
intents[intents.length-1] = rec.key.requestIntent;
rec.key.allIntents = intents;
rec.key.allResolvedTypes = resolvedTypes;
} else {
rec.key.allIntents = null;
rec.key.allResolvedTypes = null;
}
}
return rec;
}
makeIntentSenderCanceledLocked(rec);
mIntentSenderRecords.remove(key);
}
if (noCreate) {
return rec;
}
rec = new PendingIntentRecord(this, key, callingUid);
mIntentSenderRecords.put(key, rec.ref);
if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) {
if (activity.pendingResults == null) {
activity.pendingResults
= new HashSet<WeakReference<PendingIntentRecord>>();
}
activity.pendingResults.add(rec.ref);
}
return rec;
}
上面的代碼中可以看到最終返回的是PendingIntentRecord對象(既target = PendingIntentRecord),它是 IIntentSender.IIntentSender 的子類,調用者傳入的參數會構建成一個對象 PendingIntentRecord.Key,而 mIntentSenderRecords 則是一個 HashMap 緩存所有的 PendingIntentRecord。
上面用新建的 key 去 mIntentSenderRecords 查詢是否已經存在對應的 PendingIntentRecord,如存在則直接返回,否則新建一個 PendingIntentRecord 塞入 mIntentSenderRecords 并返回。
到這里稍微有一點豁然開朗,但是別急,還有一點點迷霧等待我們去撥開。
既然 HashMap 覺得 PendingIntentRecord 已經存在,那肯定是 PendingIntentRecord.Key 存在,那我們看看這個對象,它是 PendingIntentRecord 的內部類:
final static class Key {
final int type;
final String packageName;
final ActivityRecord activity;
final String who;
final int requestCode;
final Intent requestIntent;
final String requestResolvedType;
final SafeActivityOptions options;
Intent[] allIntents;
String[] allResolvedTypes;
final int flags;
final int hashCode;
final int userId;
private static final int ODD_PRIME_NUMBER = 37;
Key(int _t, String _p, ActivityRecord _a, String _w,
int _r, Intent[] _i, String[] _it, int _f, SafeActivityOptions _o, int _userId) {
// 刪掉部分無用代碼
requestIntent = _i != null ? _i[_i.length-1] : null; // 最后一個Intent
requestResolvedType = _it != null ? _it[_it.length-1] : null;
// 著重看一下 hash 生成的部分
int hash = 23;
// _f 是在 PendingIntent.getBroadcast 時傳入的 flags
hash = (ODD_PRIME_NUMBER*hash) + _f;
// _r 是在 PendingIntent.getBroadcast 時傳入的 requestCode
hash = (ODD_PRIME_NUMBER*hash) + _r;
hash = (ODD_PRIME_NUMBER*hash) + _userId;
if (_w != null) {
hash = (ODD_PRIME_NUMBER*hash) + _w.hashCode();
}
if (_a != null) {
hash = (ODD_PRIME_NUMBER*hash) + _a.hashCode();
}
// requestIntent 就是你傳入的 Intent 數組里的最后一個 Intent,
// 一般使用的時候只有一個 Intent
// 這里為里生成 hashcode, 調用里 Intent.filterHashCode()
if (requestIntent != null) {
hash = (ODD_PRIME_NUMBER*hash) + requestIntent.filterHashCode();
}
if (requestResolvedType != null) {
hash = (ODD_PRIME_NUMBER*hash) + requestResolvedType.hashCode();
}
hash = (ODD_PRIME_NUMBER*hash) + (_p != null ? _p.hashCode() : 0);
hash = (ODD_PRIME_NUMBER*hash) + _t;
hashCode = hash;
}
public int hashCode() {
return hashCode;
}
}
接著看一下 Intent.filterHashCode 的生成算法
public int filterHashCode() {
int code = 0;
if (mAction != null) {
code += mAction.hashCode();
}
if (mData != null) {
code += mData.hashCode();
}
if (mType != null) {
code += mType.hashCode();
}
if (mPackage != null) {
code += mPackage.hashCode();
}
if (mComponent != null) {
code += mComponent.hashCode();
}
if (mCategories != null) {
code += mCategories.hashCode();
}
return code;
}
那我們捋一下 PendingIntentRecord.key 的 hashcode 和我們傳入的哪些參數有關系:
記得我們上面是如何創建 PendingIntent 的嗎?
val pendingIntent1 = PendingIntent.getBroadcast(context, 0, Intent(BROADCAST_ACTION).apply {
setClass(context, BroadcastReceiver::class.java)
putExtra(key, value1)
}, PendingIntent.FLAG_UPDATE_CURRENT)
我們傳入的參數是:
context, requestCode, Intent, flags
而 Intent 又包含哪些參數呢?
private String mAction;
private Uri mData;
private String mType;
private String mPackage;
private ComponentName mComponent;
private int mFlags;
private ArraySet<String> mCategories;
private Bundle mExtras;
private Rect mSourceBounds;
private Intent mSelector;
private ClipData mClipData;
private int mContentUserHint = UserHandle.USER_CURRENT;
private String mLaunchToken;
大概是上面這些, 其中參與了生成 hashcode 就是上面提到的 filterHashCode 這個方法里的參數 所以總共參與生成 hashcode 的和調用中有關的參數是:
context, requestCode, flags, Intent.action, Intent.data, Intent.package, Intent.component, Intent.categories
再回過頭來看我是如何創建 PendingIntent 的
val pendingIntent1 = PendingIntent.getBroadcast(context, 0, Intent(BROADCAST_ACTION).apply {
setClass(context, BroadcastReceiver::class.java)
putExtra(key, value1)
}, PendingIntent.FLAG_UPDATE_CURRENT)
val pendingIntent2 = PendingIntent.getBroadcast(context, 0, Intent(BROADCAST_ACTION).apply {
setClass(context, BroadcastReceiver::class.java)
putExtra(key, value2)
}, PendingIntent.FLAG_UPDATE_CURRENT)
val pendingIntent3 = PendingIntent.getBroadcast(context, 0, Intent(BROADCAST_ACTION).apply {
setClass(context, BroadcastReceiver::class.java)
putExtra(key, value3)
}, PendingIntent.FLAG_UPDATE_CURRENT)
?? 唯一不同的是 Intent.extras,卻沒有參與 hashcode 計算,不知道是 google 故意為止還是一個小 bug ??。
總結:
- PendingIntent 中 的 target 是 PendingIntentRecord,它們都被緩存到 ActivityManagerService 中的一個HashMap中,是弱引用類型;
- 影響 PendingIntent 復用的參數主要是: requestCode, flags, Intent.action, Intent.data, Intent.package, Intent.component, Intent.categories
所以在我的做法基礎上,只要修改一下 requestCode 為不同的值就解決了。