Android安全检查之Activity劫持检测

处理检测app进去后台的时候提示用户 也就是在onPause的时候

    /**
     * Is foreground boolean.
     *
     * @param context the context
     * @return the boolean
     */
/*判断应用是否在前台*/
    public static boolean isForeground(Context context) {
        try {
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            assert am != null;
            List tasks = am.getRunningTasks(1);
            if (!tasks.isEmpty()) {
                ComponentName topActivity = tasks.get(0).topActivity;
                if (topActivity.getPackageName().equals(context.getPackageName())) {
                    return true;
                }
            }
            return false;
        } catch (SecurityException e) {
            e.printStackTrace();
            return false;
        }
    }
if (!Selfutils.isForeground(this)) {
            setShow(Config.uiStyle + "应用仍在后台运行,如需退出,请先进入"+Config.uiStyle+"应用,按手机“返回键”退出。");
        }

你可能感兴趣的:(Android,Android安全处理)