I have developed a video player application. The app will keep playing videos (and repeat them). I want to handle it while its playing so I need the videos to be playing all the time but the android system makes the screen black automatically, lock the screen or put the system on sleep. I want to know how to avoid this from happen, i.e., my program should keep playing the video and the screen shouldn't turn black. I have found some solutions on the Internet but still its not working, thanks for your help.
Asked
Active
Viewed 439 times
2 Answers
2
Try using:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
or just:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
EDIT: according to this link : How do I prevent an Android device from going to sleep programmatically?. You should use:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
..screen will stay on during this section..
wl.release();
You also need to be sure you have the WAKE_LOCK permission set in your Manifest.
-
:)it has achieve the screen keep lighten ,do you know how to avoid the screen lock and system sleep? – Honey Angry Nov 12 '12 at 08:13
0
You can use PowerManager
to avoid system sleep
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
// When app resumed or started
protected void onResume()
{
wl.acquire();
super.onResume();
}
in onPause()
you should release the wake lock, to avoid draining more battery
protected void onPause()
{
wl.release();
super.onPause();
}
And the last, don't forget the permission
<uses-permission android:name="android.permission.WAKE_LOCK"/>