Android 5.0 Screen pinning 屏幕固定功能

2015.03.13更新

恩 前几天看到android 5.1 出来了,Screen pinning这一部分有改动,具体改了什么还没看到。

这个暂时相对冷门的功能,如果你能看到这篇文章说明你还是有点了解的。

屏幕固定是android 5.0 上的新功能 其Api介绍如下:

我说一点比较重要的吧,就是开了屏幕固定以后,通知栏和状态栏会隐藏,home键和recent键会失效(单独按会失效),然后还不准启动其他activity。

就是说 你只能在这个应用内部干事情。比如你吧手机借给别人的时候就可以用这个功能 。

至于这个“尚不完善”的功能的消极作用会在文末讲一点。

-------------------------------Apis-------------------------------------------

Screen pinning

Android 5.0 introduces a new screen pinning API that lets you temporarily restrict users from leaving your task or being interrupted by notifications. This could be used, for example, if you are developing an education app to support high stakes assessment requirements on Android, or a single-purpose or kiosk application. Once your app activates screen pinning, users cannot see notifications, access other apps, or return to the home screen, until your app exits the mode.

There are two ways to activate screen pinning:

  • Manually: Users can enable screen pinning in Settings > Security > Screen Pinning, and select the tasks they want to pin by touching the green pin icon in the recents screen.
  • Programmatically: To activate screen pinning programmatically, call startLockTask() from your app. If the requesting app is not a device owner, the user is prompted for confirmation. A device owner app can call the setLockTaskPackages() method to enable apps to be pinnable without the user confirmation step.

When task locking is active, the following behavior happens:

  • The status bar is blank, and user notifications and status information are hidden.
  • The Home and Recent Apps buttons are hidden.
  • Other apps cannot launch new activities.
  • The current app can start new activities, as long as doing so does not create new tasks.
  • When screen pinning is invoked by a device owner, the user remains locked to your app until the app calls stopLockTask().
  • If screen pinning is activity by another app that is not a device owner or by the user directly, the user can exit by holding both the Back and Recent buttons.

    来源: <https://developer.android.com/about/versions/android-5.0.html#ScreenPinning>

    -------------------------reference-------------------------------------------------

    public void startLockTask ()

    Added in API level 21

    Request to put this Activity in a mode where the user is locked to the current task. This will prevent the user from launching other apps, going to settings, or reaching the home screen. If isLockTaskPermitted(String) returns true for this component then the app will go directly into Lock Task mode. The user will not be able to exit this mode until stopLockTask() is called. If isLockTaskPermitted(String) returns false then the system will prompt the user with a dialog requesting permission to enter this mode. When entered through this method the user can exit at any time through an action described by the request dialog. Calling stopLockTask will also exit the mode.

    来源: <https://developer.android.com/reference/android/app/Activity.html>

    public void stopLockTask ()

    Added in API level 21

    Allow the user to switch away from the current task. Called to end the mode started by startLockTask(). This can only be called by activities that have successfully called startLockTask previously. This will allow the user to exit this app and move onto other activities.

    来源: <https://developer.android.com/reference/android/app/Activity.html#stopLockTask()>

    --------------------------support------------------------------------------------

    Use screen pinning

    This information applies only to devices running Android 5.0 and higher.

    You can set your device to only show a certain app‘s screen using screen pinning, and some apps may ask you if you want to use screen pinning.

    Screen pinning can be handy if you want to play a game without accidentally minimizing the app if you touch the Home button. Turn on screen pinning in your device‘s settings app. Then follow the instructions below to pin a screen for a specific app.

    Turn screen pinning on or off

    Pin a screen

    Unpin a screen

    定制按键在:phoneWindowManager.java里 尤其是没有虚拟键的手机,很可能要定制按键去解除屏幕固定。

    编译后会输出到:Install: out/target/product/msm8916_64/system/framework/android.policy.jar

    ----------------------又是分割线----------

    来说消极的:

    这“然后还不准系统启动其他activity”但来一个很大的问题,就是你不能接电话了,没有任何提示,退出屏幕固定也没有来电记录。

    还有个坑爹的,如果你屏幕固定的应用是“拨号”,那么恭喜你你能拨出去电话,但是因为不准启动其他Activity,所以你看不到正在拨号和正在通话的界面,这也就意味着你没法挂断电话。

    over。

    • If you required that the lock screen show after an app is unpinned, you will need to enter your pattern, PIN, or password.

      来源: <https://support.google.com/nexus/answer/6118421?hl=en&ref_topic=3416293>

      图没挂但是要翻*墙才能看得到。

      ---------------------------

      屏幕固定相关的实现在:ActivityStackSupervisor.java


      固定

       case LOCK_TASK_START_MSG: {
                          // When lock task starts, we disable the status bars.
                          try {
                              if (mLockTaskNotify == null) {
                                  mLockTaskNotify = new LockTaskNotify(mService.mContext);
                              }
                              mLockTaskNotify.show(true);
                              mLockTaskIsLocked = msg.arg2 == 0;
                              if (getStatusBarService() != null) {
                                  int flags =
                                          StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
                                  if (!mLockTaskIsLocked) {
                                      flags ^= StatusBarManager.DISABLE_HOME
                                              | StatusBarManager.DISABLE_RECENT;
                                  }
                                  getStatusBarService().disable(flags, mToken,
                                          mService.mContext.getPackageName());
                              }
                              mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
                              if (getDevicePolicyManager() != null) {
                                  getDevicePolicyManager().notifyLockTaskModeChanged(true,
                                          (String)msg.obj, msg.arg1);
                              }
                          } catch (RemoteException ex) {
                              throw new RuntimeException(ex);
                          }
                       } break;

      解除固定

      case LOCK_TASK_END_MSG: {
                          // When lock task ends, we enable the status bars.
                          try {
                              if (getStatusBarService() != null) {
                                  getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
                                          mService.mContext.getPackageName());
                              }
                              mWindowManager.reenableKeyguard(mToken);
                              if (getDevicePolicyManager() != null) {
                                  getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
                                          msg.arg1);
                              }
                              if (mLockTaskNotify == null) {
                                  mLockTaskNotify = new LockTaskNotify(mService.mContext);
                              }
                              mLockTaskNotify.show(false);
                              try {
                                  boolean shouldLockKeyguard = Settings.System.getInt(
                                          mService.mContext.getContentResolver(),
                                          Settings.System.LOCK_TO_APP_EXIT_LOCKED) != 0;
                                  if (!mLockTaskIsLocked && shouldLockKeyguard) {
                                      mWindowManager.lockNow(null);
                                      mWindowManager.dismissKeyguard();
                                      new LockPatternUtils(mService.mContext)
                                              .requireCredentialEntry(UserHandle.USER_ALL);
                                  }
                              } catch (SettingNotFoundException e) {
                                  // No setting, don‘t lock.
                              }
                          } catch (RemoteException ex) {
                              throw new RuntimeException(ex);
                          }
                      } break;

      --------------------------------------

      如果你想自定义组合键解除屏幕固定只需要调用IActivityManager中的相应方法就行了。还有要添加android.permission.MANAGE_ACTIVITY_STACKS权限。

      IActivityManager activityManager = ActivityManagerNative.getDefault();
        if(activityManager != null && activityManager.isInLockTaskMode()){
           activityManager.stopLockTaskModeOnCurrent();
        }
    1. When you‘re on the pinned screen, touch and hold Overview  and Back  at the same time.
    2. Release both buttons and the screen will be unpinned.
    1. Make sure that screen pinning is enabled on your device.
    2. Open an app and go to the screen you want to pin.
    3. Touch Overview  on your device.
    4. Swipe up to reveal the pin icon  on the bottom right corner of your selected screen.
    5. Touch the pin icon .
    6. If you want the lock screen to appear after an app is unpinned, check the box next to "Ask for unlock pattern before unpinning."
    7. Touch Start.
    1. Open your device‘s Settings menu .
    2. Under "Personal," touch Security.
    3. Under "Advanced," touch Screen pinning.
    4. Move the switch on or off.
时间: 2024-10-11 15:40:22

Android 5.0 Screen pinning 屏幕固定功能的相关文章

[Android]Android5.0实现静默接听电话功能

原因: android曾经能够通过AIDL进行静默接听.可是5.0以后就被谷歌给屏蔽了.这时候我们仅仅能通过其它方式实现了. 解决方式: try { Runtime.getRuntime().exec("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK)); } catch (IOException e) { // Runtime.exec(String) had an I/O problem, try t

Android 6.0 系统棉花糖新的特性和功能

Get you apps ready for Android 6.0 Marshmallow! 新的功能:运行时的权限提醒,Doze(打盹模式)和备用电源,协助新技术,等等. Android 6.0Changes 伴随着新的特性和功能,Android 6.0(API level 23)在系统上和API的使用上做了一些改变. 如果我们已经发布了一款app,那么就要关注一下这些改变会不会影响应用的运行. RuntimePermissions 这是一种新的权限使用模型,用户可以在程序运行中直接管理应用

Android 5.0 Lollipop介绍

Android Lollipop介绍 Android 5.0 Lollipop,此版本为用户提供了丰富的新功能,并为开发者新增了数以千计的 API,它进一步扩展了 Android 的应用范围,从手机.平板电脑和可穿戴式设备,扩展到电视和汽车. 1.Android 5.0新增功能与特性 Material Design: 简介:Material Design语言的一些重要功能包括 系统字体Roboto的升级版本 ,同时颜色更鲜艳,动画效果更突出.杜拉特还简要谈到了新框架的一些变化--这个新框架也于今

Android 5.0 API

Android 5.0 API 在本文档中展开 更新目标 API 级别 重要的行为变更 界面 通知 图形 媒体 存储 无线和连接 Project Volta Android 在办公和教育中的应用 系统 打印框架 测试和辅助功能 IME 清单声明 API Differences API level 20 to 21 ? L Developer Preview to 21 ? See Also Android 5.0 Behavior Changes Android Lollipop Highlig

让Android手机像Win平板一样~(Android 7.0以上手机)

原帖地址 https://chaoli.club/index.php/3004 首先上图,是我的Nexus 5的屏幕截图 这是Chrome,和平时手机上的样子完全不一样,可见Chrome对不同的设备的适配是非常好的 Android 7.0新增的多窗口功能,就像一个桌面系统一样.一般来说在手机上用处不大,因为屏幕太小了,但用下面的方法就可以有图片里和平板一样的效果 首先要有一个Android 7.0系统的手机,用adb开启多窗口: adb shell settings put global ena

Android 5.0(Lollipop)中的SurfaceTexture,TextureView, SurfaceView和GLSurfaceView

SurfaceView, GLSurfaceView, SurfaceTexture以及TextureView是Android当中名字比较绕,关系又比较密切的几个类.本文基于Android 5.0(Lollipop)的代码理一下它们的基本原理,联系与区别. SurfaceView从Android 1.0(API level 1)时就有 .它继承自类View,因此它本质上是一个View.但与普通View不同的是,它有自己的Surface.我们知道,一般的Activity包含的多个View会组成Vi

Android 5.0最应该实现的8个期望

毫无疑问,Android 5 将是令人兴奋的操作系统,因为 Android4.0 至 4.4 版本之间并没有显著的差异,显然谷歌会在 5.0 版本中进行一些较大幅度的革新.那么,代号为“柠檬芝士蛋糕”或是“柠檬酥皮派”的 Android 5.0,会有什么新的特点.又会在何时发布呢?一起来看看目前我们搜集到的相关信息. 界面及功能 日前,新版本的 Android 截图已经在互联中泄露,但尚不明确是 Android 4.5 还是 5.0.可以看到,新版 Android 系统的界面更加扁平化,图标设计

Android 5.0 最应该实现的8个期望

毫无疑问,Android 5 将是令人兴奋的操作系统,因为 Android4.0 至 4.4 版本之间并没有显著的差异,显然谷歌会在 5.0 版本中进行一些较大幅度的革新.那么,代号为"柠檬芝士蛋糕"或是"柠檬酥皮派"的 Android 5.0,会有什么新的特点.又会在何时发布呢?一起来看看目前我们搜集到的相关信息. 界面及功能 日前,新版本的 Android 截图已经在互联中泄露,但尚不明确是 Android 4.5 还是 5.0.可以看到,新版 Android

Android 5.0新功能详解

2014年10月15日,Google公司发布全新的Android 操作系统Android 5.0 Lollipop(棒棒糖).距离Android系统上一次重大更新还不到一年的时间,Android从4.4 KitKat(巧克力棒)升级到了Lollipop(棒棒糖).和每年的惯例一样,Google惯例推出自家品牌Nexus phone和Nexus tablet的新产品.但最大不同是,Android Lollipop的发布成为Android系统有史以来变化最大的一次升级. 12个独特之处,带你快速了解