简体   繁体   English

FLAG_KEEP_SCREEN_ON反向工作

[英]FLAG_KEEP_SCREEN_ON working in reverse

This is an odd one. 这是一个奇怪的。

I was previously using: android:keepScreenOn="true" 我以前使用的是: android:keepScreenOn="true"

in my layout file to keep the screen on in an activity and this worked fine. 在我的布局文件中以保持屏幕处于活动状态,这很好用。

However I wanted to improve the so that the screen is only kept on when the web app is in a particular state. 但是我想改进它,以便仅在Web应用程序处于特定状态时才保持屏幕打开。 (The user should still be able to turn the screen off if they wish, I effectively just want to disable the timeout - which the above in the layout file achieves whenever the activity is active.) (用户仍然可以根据需要关闭屏幕,我实际上只是想禁用超时-每当活动处于活动状态时,布局文件中的超时都可以实现。)

To do this I have a JavaScript interface add to the WebView which casks the following two methods. 为此,我在WebView中添加了一个JavaScript接口,该接口包含以下两种方法。

I have commented out the keepScreenOn setting in the layout. 我已注释掉布局中的keepScreenOn设置。

@JavascriptInterface
public void keepScreenOn(){
    Toast.makeText(mContext, "Keeping screen on", Toast.LENGTH_LONG).show();
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
@JavascriptInterface
public void allowScreenOff(){
    Toast.makeText(mContext, "Allowing screen off", Toast.LENGTH_LONG).show();
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

Now the correct Toast is displayed, but the "keep screen on" action is reversed. 现在,将显示正确的Toast ,但是“保持屏幕打开”操作将被反转。

ie when I get the Toast saying the screen will be kept on it times out and switches of after a couple of minutes. 即,当我收到Toast的消息时,屏幕将一直停留在屏幕上,并在几分钟后切换。 BUT when I get the Toast saying that the screen will be allowed to turn off the screen actually stays on without ever timing out. 但是,当我Toast话说,将允许关闭屏幕时,屏幕实际上会一直保持打开状态,而不会超时。

I don't understand how this can be. 我不知道怎么回事。


OK, the plot thickens. 好,情节变厚。

It seems it may not always be a reversal of the operation, but could be a random work/not working situation. 看来这不一定是操作的逆转,但可能是随机工作/不工作的情况。

Also, looking in the log in Android Studio, I'm getting an error that says: 另外,在Android Studio中查看日志时,出现一条错误消息:

01-26 20:22:51.358 2182-5629/com.nooriginalthought.bluebadgeparking E/ViewRootImpl: com.nooriginalthought.bluebadgeparking.websiteViewActivity : Only the original thread that created a view hierarchy can touch its views.

when the method is called. 调用该方法时。

BUT it does work sometimes. 但是有时候确实可以。

The methods shown above are within the websiteViewActivity class, but are not within the onCreate method. 上面显示的方法在websiteViewActivity类中,但不在onCreate方法中。 I'm afraid I don't know if this is relevant or how to ensure the addFlags and clearFlags are run in the correct thread if it is relevant. 恐怕我不知道这是否相关,或者如果相关,如何确保addFlagsclearFlags在正确的线程中运行。

[Edit: This question explains how to get things running on the UI thread and has got rid of the 'Only the original thread' error.] [编辑: 此问题说明了如何使事情在UI线程上运行,并且摆脱了“仅原始线程”错误。]

And why does it sometimes work (I can't say if the error show up in the log when it works or not as I've only seen it working when testing away from my PC)? 以及为什么有时它会起作用(我不能说该错误是否在工作时出现在日志中,因为我只是在离开PC进行测试时才看到它在工作)?

The text "Only the original thread that created a view hierarchy can touch its views" means that you are executing this code from non-main thread, which is also called a UI Thread. 文本“只有创建视图层次结构的原始线程才能触摸其视图”表示您正在从非主线程(也称为UI线程)执行此代码。

The WebView usually executes javascript in a background thread so that's why you get this error. WebView通常在后台线程中执行javascript,因此这就是您收到此错误的原因。

Try using "runOnUiThread()" 尝试使用“ runOnUiThread()”

runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(mContext, "Keeping screen on", Toast.LENGTH_LONG).show();
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    });
private Window wind;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  //your code

    wind = this.getWindow();
    wind.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    wind.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    wind.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    wind.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

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

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