Keep the Screen On
First way:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Second way:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
...
</RelativeLayout>
Keep the CPU On:
<uses-permission android:name="android.permission.WAKE_LOCK" />
and
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag");
wakeLock.acquire();
To release the wake lock, call wakelock.release()
. This releases your claim to the CPU. It's important to release a wake lock as soon as your app is finished using it to avoid draining the battery.
Docs here.