音乐播放过程中,在设置-》应用中,对音乐执行强制停止,相关日志如下:
framework层相关代码alps\frameworks\base\services\core\java\com\android\server\am\ActivityManagerService.java中对应用停止运行执行的代码如下:
@Override
public void forceStopPackage(final String packageName, int userId) {
if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
!= PackageManager.PERMISSION_GRANTED) {
String msg = "Permission Denial: forceStopPackage() from pid="
+ Binder.getCallingPid()
+ ", uid=" + Binder.getCallingUid()
+ " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
Slog.w(TAG, msg);
throw new SecurityException(msg);
}
final int callingPid = Binder.getCallingPid();
userId = mUserController.handleIncomingUser(callingPid, Binder.getCallingUid(),
userId, true, ALLOW_FULL_ONLY, "forceStopPackage", null);
long callingId = Binder.clearCallingIdentity();
try {
IPackageManager pm = AppGlobals.getPackageManager();
synchronized(this) {
int[] users = userId == UserHandle.USER_ALL
? mUserController.getUsers() : new int[] { userId };
for (int user : users) {
int pkgUid = -1;
try {
pkgUid = pm.getPackageUid(packageName, MATCH_DEBUG_TRIAGED_MISSING,
user);
} catch (RemoteException e) {
}
if (pkgUid == -1) {
Slog.w(TAG, "Invalid packageName: " + packageName);
continue;
}
try {
pm.setPackageStoppedState(packageName, true, user);
} catch (RemoteException e) {
} catch (IllegalArgumentException e) {
Slog.w(TAG, "Failed trying to unstop package "
+ packageName + ": " + e);
}
/// M: [process suppression] @{
if ("1".equals(SystemProperties.get("persist.runningbooster.support")) ||
"1".equals(SystemProperties.get("ro.mtk_aws_support"))) {
AMEventHookData.PackageStoppedStatusChanged eventData =
AMEventHookData.PackageStoppedStatusChanged.createInstance();
eventData.set(packageName, SUPPRESS_ACTION_STOP, "forceStopPackage");
mAMEventHook.hook(AMEventHook.Event.AM_PackageStoppedStatusChanged,
eventData);
}
/// M: [process suppression] @}
if (mUserController.isUserRunningLocked(user, 0)) {
forceStopPackageLocked(packageName, pkgUid, "from pid " + callingPid);
finishForceStopPackageLocked(packageName, pkgUid);
}
}
}
} finally {
Binder.restoreCallingIdentity(callingId);
}
}
在设置中对音乐点击强制停止,如果想让音乐继续后台播放,可以在ActivityManagerService.java中对代码进行控制,判断packageName是否为音乐,如果是音乐,直接返回即可。到时产生的效果是,点击强制停止,会没有响应,达到了想要的效果。