0

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.

Echilon
  • 10,064
  • 33
  • 131
  • 217
Honey Angry
  • 61
  • 1
  • 8

2 Answers2

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.

Community
  • 1
  • 1
omi0301
  • 473
  • 2
  • 5
  • 15
0

You can use PowerManager to avoid system sleep

How to use PowerManager

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"/>