There are certain situations when we need to turn screen ON such as live map games, routing, movie, tv apps.
Basically, to avoid battery draining android device fall asleep after some time, but to accomplish this task we have to keep screen ON.
We can do it from both Activity as well as from XML.
From Activity:
We use WindowManager class by using FLAG_KEEP_SCREEN_ON property.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
From XML:
By using keepScreenOn attribute
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
...
</RelativeLayout>
0 Comment(s)