[英]How do I keep the screen on in any activity or fragment in my App by using kotlin?
I wanted to keep the light of my app's screen always on when I go to a certain activity or fragment, but I can't do that.当我 go 到某个活动或片段时,我想保持我的应用程序屏幕始终亮着,但我不能这样做。
You should use this concept WakeLock or Keep Screen On您应该使用此概念WakeLock 或 Keep Screen On
There are two things 1)If you need to make activity on keep on you can use Keep screen on.有两件事 1)如果您需要在保持上进行活动,您可以使用保持屏幕上。 It does not require any special permission.
它不需要任何特殊许可。 You can do this in activity or xml.
您可以在活动或 xml 中执行此操作。 Activity:
活动:
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
//Make sure call this function to clear flags when you dont need this in between .
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
XML: XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_parent"
android:keepScreenOn="true"></RelativeLayout>
Second way - if you need to do some CPU running.第二种方式 - 如果你需要做一些 CPU 运行。 Example like a game app.
游戏应用程序之类的示例。 You need to declare permission in manifest.
您需要在清单中声明权限。
And follow this example.并按照 这个例子。 Its a straight forward way.
这是一个直截了当的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.