How to check if an activity is locked (app pinning

2020-08-13 05:51发布

问题:

I would like to know whether an activity is locked under app pinning in android 5.0 and above programatically. Please help me in this!

Thanks!

回答1:

Method to get if the activity in lock task mode.
activityManager.isInLockTaskMode() API is deprecated in API level 23. Use the method activityManager.getLockTaskModeState() http://developer.android.com/reference/android/app/ActivityManager.html#getLockTaskModeState()

public boolean isAppInLockTaskMode() {
    ActivityManager activityManager;

    activityManager = (ActivityManager)
        this.getSystemService(Context.ACTIVITY_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // For SDK version 23 and above.
        return activityManager.getLockTaskModeState()
            != ActivityManager.LOCK_TASK_MODE_NONE; 
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // When SDK version >= 21. This API is deprecated in 23.
        return activityManager.isInLockTaskMode();
    }

    return false;
}

Hope this helps you!