简体   繁体   English

编译和运行项目时唤醒和解锁Android手机屏幕?

[英]Wake and unlock Android phone screen when compile and run project?

When build and run iOS app using Xcode, the phone becomes awake and app runs. 使用Xcode构建和运行iOS应用程序时,手机会变得清醒并且应用运行。 Is there a way to wake and unlock Android phone (or tablet) screen then run installed Android app (Eclipse, Android Studio)? 有没有办法唤醒和解锁Android手机(或平板电脑)屏幕,然后运行已安装的Android应用程序(Eclipse,Android Studio)?

One solution: set the following flags in your activity class(es): 一种解决方案:在您的活动类中设置以下标志:

    if (BuildConfig.DEBUG) {
        // These flags cause the device screen to turn on (and bypass screen guard if possible) when launching.
        // This makes it easy for developers to test the app launch without needing to turn on the device
        // each time and without needing to enable the "Stay awake" option.
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    }

These flags will: 这些标志将:

  1. Turn on the screen 打开屏幕
  2. Bypass the lock-screen 绕过锁屏
  3. Allow showing the activity even if the device is locked 即使设备已锁定,也允许显示活动

By setting those flags in your base activity, you'll be able to continue using your application even if the device was off and/or locked at the time of running. 通过在基本活动中设置这些标志,即使设备在运行时关闭和/或锁定,您也可以继续使用您的应用程序。 If you try to leave your application's process (ie, hitting the home button, or switching to another app), the lock screen will appear, and you'll have to manually unlock to continue using the device. 如果您尝试离开应用程序的过程(即,按下主页按钮或切换到另一个应用程序),将出现锁定屏幕,您必须手动解锁才能继续使用该设备。

Warning : this should only be used while developing/debugging your application, so I recommend keeping the if (BuildConfig.DEBUG) check, as it is in this example 警告 :这应该仅在开发/调试应用程序时使用,因此我建议保留if (BuildConfig.DEBUG)检查,就像在本例中一样

Better and easiest solution is use the option (in development section) that the screen never goes off I thing the name is "Stay awake". 更好,更简单的解决方案是使用选项(在开发部分),屏幕永远不会关闭我的名字是“保持清醒”。 This option prevent your phone to get locked when the usb cable is connected. 此选项可防止在连接USB电缆时锁定手机。

您无法解锁并唤醒手机,因为这不是很安全,但您可以在开发人员选项中启用“保持清醒”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM