0

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.

Fahad
  • 1
  • 2

1 Answers1

0

You should use this concept WakeLock or Keep Screen On

There are two things 1)If you need to make activity on keep on you can use Keep screen on. It does not require any special permission. You can do this in activity or 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:

<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 . Example like a game app. You need to declare permission in manifest.

And follow this example. Its a straight forward way.

Gowtham K K
  • 3,123
  • 1
  • 11
  • 29