Android9.0 Launcher3中Recents(多任务),全部清除 按钮点击事件

Android 9.0 与Android 8.1 中的点击Recent键清除按钮有些区别,之前Android 8.1 的全部清除按钮位于SystemUI中,而Android 9.0 的“全部清除”按钮位于Launcher3中;
估计是为了函数分离做的修改, 由于SystemUI中保留了大部分的代码,在调试时候,产生了部分误导的情况,直到调试发现了该问题点;

Launcher3\quickstep\src\com\android\quickstep\views\RecentsView.java
其中函数为:

private void removeTask(Task task, int index, PendingAnimation.OnEndListener onEndListener,
                        boolean shouldLog) {
    if (task != null) {
        ActivityManagerWrapper.getInstance().removeTask(task.key.id);
        if (shouldLog) {
            mActivity.getUserEventDispatcher().logTaskLaunchOrDismiss(
                    onEndListener.logAction, Direction.UP, index,
                    TaskUtils.getLaunchComponentKeyForTask(task.key));
        }
        if (task.key.getComponent() == null || TextUtils.isEmpty(task.key.getComponent().getPackageName())) {
            return;
        }
        
        stopApp(getContext(), task.key.getComponent().getPackageName());
    }
}

增加的清除对应PackageName 的函数方法为:

public boolean stopApp(Context context, String pkgname) {
    boolean flag = false;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    try {
        Method forceStopPackage;
        forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage",
                String.class); //
        forceStopPackage.setAccessible(true);
        forceStopPackage.invoke(am, pkgname);
        flag = true;
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        flag = false;
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        flag = false;
    } catch (InvocationTargetException e) {
        e.printStackTrace();
        flag = false;
    } catch (SecurityException e) {
        e.printStackTrace();
        flag = false;
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
        flag = false;
    }
    return flag;
}

注意在AndroidManifest.xml中增加对应的权限:

<uses-permission android:name="android.permission.FORCE_STOP_PACKAGES" />

猜你喜欢

转载自blog.csdn.net/liuminx/article/details/106619498