- 问题描述:点击Navigation bar的recent key在最近应用程序列表中会显示最近访问过的应用,如 何做到不显示某应用以达到隐藏进程的目的呢?
1.应用层处理
为该应用AndroidManifest xml文件中主activity设置属性
android:excludeFromRecents=“true”
例如:
<activity android:name=".HttpTest"
android:excludeFromRecents="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
应用是否具有android.intent.category.LAUNCHER属性有关,在主Activity有LAUNCHER的前提下,android:excludeFromRecents=“true”,才能达到在最近任务列表中隐藏该应用的目的。
2.framework层处理
- 源码地址:frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
@Override
public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, int flags, int userId) {
final int callingUid = Binder.getCallingUid();
userId = handleIncomingUser(Binder.getCallingPid(), callingUid, userId,
false, ALLOW_FULL_ONLY, "getRecentTasks", null);
final boolean includeProfiles = (flags & ActivityManager.RECENT_INCLUDE_PROFILES) != 0;
final boolean withExcluded = (flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0;
synchronized (this) {
final boolean allowed = isGetTasksAllowed("getRecentTasks", Binder.getCallingPid(), callingUid);
final boolean detailed = checkCallingPermission(
android.Manifest.permission.GET_DETAILED_TASKS) == PackageManager.PERMISSION_GRANTED;
final int recentsCount = mRecentTasks.size();
/// M: ALPS01250694, add for recent task debug @{
if (DEBUG_TASKS) {
Slog.i(TAG, "getRecentTasks recentsCount = " + recentsCount +
" maxNum = " + maxNum + " flags = " + flags);
}
/// @}
ArrayList<ActivityManager.RecentTaskInfo> res =
new ArrayList<>(maxNum < recentsCount ? maxNum : recentsCount);
final Set<Integer> includedUsers;
if (includeProfiles) {
includedUsers = getProfileIdsLocked(userId);
} else {
includedUsers = new HashSet<>();
}
includedUsers.add(Integer.valueOf(userId));
for (int i = 0; i < recentsCount && maxNum > 0; i++) {
TaskRecord tr = mRecentTasks.get(i);
/// M: ALPS01250694, add for recent task debug @{
if (DEBUG_TASKS) {
Slog.i(TAG, "getRecentTasks tr = " + tr);
}
/// @}
// Only add calling user or related users recent tasks
if (!includedUsers.contains(Integer.valueOf(tr.userId))) {
if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Skipping, not user: " + tr);
continue;
}
// Return the entry if desired by the caller. We always return
// the first entry, because callers always expect this to be the
// foreground app. We may filter others if the caller has
// not supplied RECENT_WITH_EXCLUDED and there is some reason
// we should exclude the entry.
if (i == 0
|| withExcluded
|| (tr.intent == null)
|| ((tr.intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
== 0)) {
if (!allowed) {
// If the caller doesn't have the GET_TASKS permission, then only
// allow them to see a small subset of tasks -- their own and home.
if (!tr.isHomeTask() && tr.effectiveUid != callingUid) {
if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Skipping, not allowed: " + tr);
continue;
}
}
if ((flags & ActivityManager.RECENT_IGNORE_HOME_STACK_TASKS) != 0) {
if (tr.stack != null && tr.stack.isHomeStack()) {
if (DEBUG_RECENTS) Slog.d(TAG_RECENTS,
"Skipping, home stack task: " + tr);
continue;
}
}
if (tr.autoRemoveRecents && tr.getTopActivity() == null) {
// Don't include auto remove tasks that are finished or finishing.
if (DEBUG_RECENTS) Slog.d(TAG_RECENTS,
"Skipping, auto-remove without activity: " + tr);
continue;
}
if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0
&& !tr.isAvailable) {
if (DEBUG_RECENTS) Slog.d(TAG_RECENTS,
"Skipping, unavail real act: " + tr);
continue;
}
ActivityManager.RecentTaskInfo rti = createRecentTaskInfoFromTaskRecord(tr);
if (!detailed) {
rti.baseIntent.replaceExtras((Bundle)null);
}
- res.add(rti);
+ // add for hide app in recent-list
+ ComponentName cn = tr.intent.getComponent();
+ String marketName = SystemProperties.get("market.name");
+ if (cn != null && cn.getPackageName().equals(marketName)) {
+ Log.d(TAG," name "+cn.getPackageName());
+ }else {
+ res.add(rti);
+ }
+ // end
maxNum--;
}
}
return res;
}
}
这里我把源码中获取recent-list源码都贴出来了,方便阅读