<uses-permission android:name="android.permission.WAKE_LOCK" />
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag"); wakeLock.acquire();
wakelock.release()
keep wakeLock for whole app
public class MyApp extends Application { private static final String TAG = MyApp.class.getSimpleName(); private PowerManager.WakeLock mWakeLock = null; @Override public void onCreate() { super.onCreate(); final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); mWakeLock.acquire(); } @Override public void onTerminate() { if (mWakeLock.isHeld()) mWakeLock.release(); super.onTerminate(); } }
or
Use a broadcast receiver that keeps the device awake
References
https://developer.android.com/training/scheduling/wakelock
https://developer.android.com/topic/performance/vitals/wakelock
https://stackoverflow.com/questions/9000563/partial-wake-lock-is-not-working