Android 5.0 Screen pinning 屏幕固定功能

2015.03.13更新html

恩 前幾天看到android 5.1 出來了,Screen pinning這一部分有改動,具體改了什麼還沒看到。java


這個暫時相對冷門的功能,若是你能看到這篇文章說明你仍是有點了解的。android

屏幕固定是android 5.0 上的新功能 其Api介紹以下:
ios

我說一點比較重要的吧,就是開了屏幕固定之後,通知欄和狀態欄會隱藏,home鍵和recent鍵會失效(單獨按會失效),而後還不許啓動其餘activity。app

就是說 你只能在這個應用內部幹事情。好比你吧手機借給別人的時候就能夠用這個功能 。ide

至於這個「尚不完善」的功能的消極做用會在文末講一點。ui

-------------------------------Apis-------------------------------------------this

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.google

There are two ways to activate screen pinning:spa

  • 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.

相關文章
相關標籤/搜索