I have an Android application and I want to keep the screen on. But sometimes I don't want to keep the screen on. What can I do to that?
EDIT: I have already used this code to set my screen on:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
See the docs - either
android:keepScreenOn="true"
in layout or programatically
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
https://developer.android.com/training/scheduling/wakelock.html
Your question is not clear, but I guess you are looking for this. You can do this grammatically.
or Add this to your layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
...
</RelativeLayout>
Find the root layout of the activity. for example if the root layout is a LinearLayout
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xml);
LinearLayout root = (LinearLayout) findViewById(R.id.root_container):
// and according to your preference you can set
root.setKeepScreenOn(true/false);
offical doc says
/**
* Controls whether the screen should remain on, modifying the
* value of {@link #KEEP_SCREEN_ON}.
*
* @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
*
* @see #getKeepScreenOn()
*/
For more reference please follow this link
当您需要应用程序不打开屏幕时,可以使用以下代码清除FLAG_KEEP_SCREEN_ON
标志:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
You should use this:
<uses-permission android:name="android.permission.WAKE_LOCK" />
and then :
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag");
wakeLock.acquire();
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.