سؤال

I want phone screen to be always on and keep brightness which I set all the time while app is running.

This is the code to have screen always on

PowerManager pm;
PowerManager.WakeLock wl;
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();

And this is the code to set brightness:

WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = 1F;
getWindow().setAttributes(layout);

Now the screen is always on but after some time the brightness is lowered. And it stays on with this low brightness. How can I make the screen brightness to stay the same all the time while application is running.

هل كانت مفيدة؟

المحلول

Use this in your layout or in the Manifest for your Activity

android:keepScreenOn="true"

With this you can get rid of the WakeLock and the Brightness adjustment because the screen will always be on with the brightness level setted by the device as long the Activity is in foreground.

نصائح أخرى

use this in your onResume()

getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

and this in your onPause()

getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top