设备屏幕关闭/进入睡眠状态时手电筒不工作(android studio)

编程入门 行业动态 更新时间:2024-10-22 16:50:59
本文介绍了设备屏幕关闭/进入睡眠状态时手电筒不工作(android studio)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了一个手电筒应用程序,手电筒正常工作.但是当设备屏幕电源关闭/处于睡眠状态时,手电筒不起作用.当设备屏幕处于关闭/睡眠状态时,我希望手电筒继续打开..

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)

本文发布于:2023-11-27 12:16:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638039.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:手电筒   睡眠   屏幕   状态   设备

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!