相信不少使用Nexus6p或者Pixel手机作为开发机的同学在升级到Android7.1.1版本后会发现在通知栏莫名其妙的会多了很多通知栏,如下图所示:
其中最上面那一条黑色的是手Q的,下面的几个大家都能看到名字。笔者当时第一反应就是我们常规的进程保活方案之一,也就是启动Foreground Service并隐藏Notification的方案在新系统中失效了。同时也能发现,手Q和美团以及QQ音乐等应用都采用了该保活方案。对该方案不熟悉的同学,可以参考这里。
那么,Google到底做了些什么让上述方案失效了呢?好在Google已经发布了Android25的源码,具体原因嘛,只能通过RTFSC (Read the fucking source code)来寻找了。
首先,我们知道,要隐藏Notification,我们需要调用stopForeground(true),跟进去这个方法
public final void stopForeground(boolean removeNotification) {
stopForeground(removeNotification ?STOP_FOREGROUND_REMOVE : 0);
}
继续跟进
public final void stopForeground(@StopForegroundFlags int flags) {
try {
mActivityManager.setServiceForeground(
new ComponentName(this, mClassName), mToken, 0, null, flags);
} catch (RemoteException ex) {
}
}
发现最终调用的是mActivityManager的setServiceForeground方法,这里的flags的值为STOP_FOREGROUND_REMOVE。
好,我们继续跟进setServiceForeground()方法。这里先找到mActivityManager的声明,如下:
private IActivityManager mActivityManager = null;
发现新的目标IActivityManager,继续跟进,发现IActivityManager是一个接口,而setServiceForeground()方法是一个空的实现,如下:
public void setServiceForeground(ComponentName className, IBinder token,
int id, Notification notification, int flags) throws RemoteException;
这个时候,你会发现,跟进到这里已经不能在Android Studio中继续跟进了,因为在Android Studio中找不到它的实现类。在这里向大家推荐阅读Android源码的好工具--**Source Insight
** 。
终于,我们找到了IActivityManager的实现类android.app.ActivityManagerNative(具体过程这里就不展开了),查看它对setServiceForeground()方法的具体实现,如下:
public void setServiceForeground(ComponentName className, IBinder token,
int id, Notification notification, int flags) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
data.writeInterfaceToken(IActivityManager.descriptor);
ComponentName.writeToParcel(className, data);
data.writeStrongBinder(token);
data.writeInt(id);
if (notification != null) {
data.writeInt(1);
notification.writeToParcel(data, 0);
} else {
data.writeInt(0);
}
data.writeInt(flags);
mRemote.transact(SET_SERVICE_FOREGROUND_TRANSACTION, data, reply, 0);
reply.readException();
data.recycle();
reply.recycle();
}
可以看出,这里仍然没有做实际操作,而是通过Binder机制调用远端的服务(对Android Binder机制不了解的同学请查阅相关资料,这里不展开讨论)。貌似线索又断了,这里的服务端到底是哪个类呢?这个时候Source Insight又能发挥它的作用了,我们很快找到com.android.server.am.ActivityManagerService这个类继承自ActivityManagerNative并重写了setServiceForeground()方法,如下
@Override
public void setServiceForeground(ComponentName className, IBinder token,
int id, Notification notification, int flags) {
synchronized(this) {
mServices.setServiceForegroundLocked(className, token, id, notification, flags);
}
}
哈,又重新找到线索了。可以看到,最终调用到了mServices的setServiceForegroundLocked()方法。那么,mServices又是什么呢?
可以找到mServices的声明如下:
final ActiveServices mServices;
好,我们继续跟进ActiveServices类,该类的完整类名是:com.android.server.am.ActiveServices,它的setServiceForegroundLocked()方法的定义如下:
public void setServiceForegroundLocked(ComponentName className, IBinder token,
int id, Notification notification, int flags) {
final int userId = UserHandle.getCallingUserId();
final long origId = Binder.clearCallingIdentity();
try {
ServiceRecord r = findServiceLocked(className, token, userId);
if (r != null) {
if (id != 0) {
if (notification == null) {
throw new IllegalArgumentException("null notification");
}
if (r.foregroundId != id) {
cancelForegroudNotificationLocked(r);
r.foregroundId = id;
}
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
r.foregroundNoti = notification;
r.isForeground = true;
r.postNotification();
if (r.app != null) {
updateServiceForegroundLocked(r.app, true);
}
getServiceMap(r.userId).ensureNotStartingBackground(r);
mAm.notifyPackageUse(r.serviceInfo.packageName,
PackageManager.NOTIFY_PACKAGE_USE_FOREGROUND_SERVICE);
} else {
if (r.isForeground) {
r.isForeground = false;
if (r.app != null) {
mAm.updateLruProcessLocked(r.app, false, null);
updateServiceForegroundLocked(r.app, true);
}
}
//注意这里的STOP_FOREGROUND_REMOVE
if ((flags & Service.STOP_FOREGROUND_REMOVE) != 0) {
cancelForegroudNotificationLocked(r);
r.foregroundId = 0;
r.foregroundNoti = null;
} else if (r.appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
r.stripForegroundServiceFlagFromNotification();
if ((flags & Service.STOP_FOREGROUND_DETACH) != 0) {
r.foregroundId = 0;
r.foregroundNoti = null;
}
}
}
}
} finally {
Binder.restoreCallingIdentity(origId);
}
}
这个方法代码比较多,大家可以重点关注我注释的地方。还记得前面我提到的STOP_FOREGROUND_REMOVE这个flag吗?啊哈,看来我们终于找到地方了。看到这里,cancelForegroudNotificationLocked()方法有应该就是我们要找的地方了,继续跟进去,果不其然,在这里执行了真正的操作,代码如下:
private void cancelForegroudNotificationLocked(ServiceRecord r) {
if (r.foregroundId != 0) {
// First check to see if this app has any other active foreground services
// with the same notification ID. If so, we shouldn't actually cancel it,
// because that would wipe away the notification that still needs to be shown
// due the other service.
ServiceMap sm = getServiceMap(r.userId);
if (sm != null) {
for (int i = sm.mServicesByName.size()-1; i >= 0; i--) {
ServiceRecord other = sm.mServicesByName.valueAt(i);
if (other != r && other.foregroundId == r.foregroundId
&& other.packageName.equals(r.packageName)) {
// Found one! Abort the cancel.
return;
}
}
}
r.cancelNotification();
}
}
现在一切真像大白,原来这里系统会进行判断,如果当前还有相同id的Notification存在,则直接就返回了!!!大家看上面方法里的注释就是这个意思。
结论
虽然隐藏Foreground Service的Notification在Android SDK 25上失效了,但是保活效果不受影响。