Android在应用程式中保持萤幕开启 [英] Android Keep Screen On In App

查看:133
本文介绍了Android在应用程式中保持萤幕开启的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行前台服务的android应用,当该服务运行时,我希望为用户提供保持屏幕打开的选项.

I have an android app which runs a foreground service and while that service is running I would like to give the user the option to keep the screen on.

我在设置首选项中添加了一个复选框首选项,如果为true,我想保持屏幕打开,但默认情况下将其设置为关闭.这只是我的用户所要求的.目前我有偏好,但是当我运行该服务时,我的屏幕仍然关闭,这是我已经完成的事情

I added to my settings preferences, a checkbox preference and if true, I would like to keep the screen on but have it set to off by default. It's just something my users have asked for. Currently i have the preference on but when I run the service my screen still shuts of here is what I've done

全局变量

private static final String PREFS_DEVICE = "DeviceInfo";

代码和if语句

SharedPreferences settings = getSharedPreferences(PREFS_DEVICE, 0);
            if(settings.getBoolean("screenPref", false)) {
                final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
                this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
                this.mWakeLock.acquire();
            }

,然后我将其添加到清单中

<uses-permission android:name="android.permission.WAKE_LOCK" />

我在做错什么吗?是否有通过服务执行此操作的其他方法,或者只是无法通过服务执行此操作(希望不是这种情况).

Is there something I'm doing wrong. Is there a different way to do this from a service or is it just not possible from a service (hope that's not the case).

推荐答案

尝试一下:

mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,         
                            "");    
mWakeLock.acquire();

这篇关于Android在应用程式中保持萤幕开启的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆