android.app.ActivityManager#isInLockTaskMode ( )源码实例Demo

下面列出了android.app.ActivityManager#isInLockTaskMode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: android-testdpc   文件: KioskModeActivity.java
@Override
protected void onStart() {
    super.onStart();

    // start lock task mode if it's not already active
    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    // ActivityManager.getLockTaskModeState api is not available in pre-M.
    if (Util.SDK_INT < VERSION_CODES.M) {
        if (!am.isInLockTaskMode()) {
            startLockTask();
        }
    } else {
        if (am.getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_NONE) {
            startLockTask();
        }
    }
}
 
@TargetApi(VERSION_CODES.P)
private void relaunchInLockTaskMode() {
    ActivityManager activityManager = getContext().getSystemService(ActivityManager.class);

    final Intent intent = new Intent(getActivity(), getActivity().getClass());
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    // Ensure a new task is actually created if not already running in lock task mode
    if (!activityManager.isInLockTaskMode()){
        intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    }

    final ActivityOptions options = ActivityOptions.makeBasic();
    options.setLockTaskEnabled(true);

    try {
        startActivity(intent, options.toBundle());
        getActivity().finish();
    } catch (SecurityException e) {
        showToast("You must first whitelist the TestDPC package for LockTask");
    }
}