问题描述
我创建了一个手电筒应用程序,手电筒正常工作.但是当设备屏幕电源关闭/处于睡眠状态时,手电筒不起作用.当设备屏幕处于关闭/睡眠状态时,我希望手电筒继续打开..
i created a flashlight application,flashlight working. but flashlight not work when device screen power is off/sleep.i want flashlight continue on ,when device screen in off/sleep..
我是这个网站的新手,我不知道如何上传完整代码,所以我将代码上传到我的博客网站
i am new for this site, i don't know,how upload full code,so i upload code in my blog site
manifest code
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:noHistory="true" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".About" android:noHistory="true" android:screenOrientation="portrait" >
</activity>
</application>
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-feature android:name="android.hardware.Camera"></uses-feature>
推荐答案
在if(!isOn)块内添加以下代码:
Add below code inside if(!isOn) block:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
并且屏幕将永远不会休眠.然后在该语句的else块中,添加以下代码,以重新启用睡眠功能:
And the screen will never sleep.Then in the else block of that statement, add below code, which reenables the sleep feature:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
如果您最后使用onStop()方法在用户关闭应用程序时关闭相机,请添加相同的代码 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
在那个街区也是如此.
If you use onStop() method at the end for turning camera off when user closes the app, add the same code getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
In that block too.
这篇关于设备屏幕关闭/进入睡眠状态时手电筒不工作(android studio)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!