简体   繁体   English

如何使用 kotlin 在我的应用程序中的任何活动或片段中保持屏幕开启?

[英]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>
  1. 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.

相关问题 如何解决我的 kotlin 应用程序的黑屏问题? - How do I solve this blank screen issue of my kotlin app? 如何在 Kotlin 的片段中获取不是 null 和 runOnUIThread() 的活动? - How do I get the activity that is not null and runOnUIThread() within a Fragment in Kotlin? 在 Kotlin 如何在打开活动的片段中创建按钮? - In Kotlin how do I create a button in a fragment that open an activity? 如何从 Kotlin 中的片段 A 开始活动 B? - How do I start activity B from fragment A in Kotlin? 如何使用Kotlin从片段内部访问活动中的SQLite DB? - How do I access SQLite DB in an activity, from inside a fragment using Kotlin? 如何使用后台运行的应用程序使屏幕保持打开状态? - How can I keep my screen on using a background running app? Android开发:当我的应用程序处于后台时,如何保持屏幕显示状态? - Android Development: How can I keep screen on when the activity of my app is at background? 如何保持当前片段在屏幕上旋转 - How to keep my current fragment on screen rotation 尝试将包从我的活动传递到我的片段时,为什么我不断收到 null? - Why do I keep getting null when trying to pass a bundle from my activity to my fragment? 如何从 Kotlin 的设置活动中检索首选项? - How do I retrieve the preferences from my settings activity in Kotlin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM