判断当前的Activity的是否处于栈顶

lockAppName 是需要判断Activity的全称(包括包名).

private boolean getTopApp(Context mContext) {
        String lockAppName = "com.celltick.lockscreen.LockerActivity";
        String topActivityName = "";
        ActivityManager am = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> runningTasks = am
                .getRunningTasks(1);
        if (runningTasks != null && !runningTasks.isEmpty()) {
            ActivityManager.RunningTaskInfo taskInfo = runningTasks.get(0);
            topActivityName = taskInfo.topActivity.getClassName();
        }
        if (lockAppName.equals(topActivityName)) {
            return true;
        }
        return false;
    }

 

posted on 2017-05-10 17:26  大米稀饭  阅读(872)  评论(0编辑  收藏  举报