PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); wl.acquire(); // screen and CPU will stay awake during this section wl.release();
@Override public void serviceProcessingStarted() { synchronized (this) { // We hold the wake lock as long as the service is processing commands. if (!mServiceProcessing) { mServiceProcessing = true; // Keep the device awake, but only for at most 10 minutes at a time // (Similar to JobScheduler.) if (mRunWakeLock != null) { mRunWakeLock.acquire(10 * 60 * 1000L); } if (mLaunchWakeLock != null) { mLaunchWakeLock.release(); } } } }
@Override public void serviceProcessingFinished() { synchronized (this) { if (mServiceProcessing) { // If we are transitioning back to a wakelock with a timeout, do the same // as if we had enqueued work without the service running. if (mLaunchingService && mLaunchWakeLock != null) { mLaunchWakeLock.acquire(60 * 1000); } mServiceProcessing = false; if (mRunWakeLock != null) { mRunWakeLock.release(); } } } } }
package mypackage.test; import android.content.Context; import android.os.PowerManager; public abstract class WakeLocker { private static PowerManager.WakeLock wakeLock; public static void acquire(Context ctx) { if (wakeLock != null) wakeLock.release(); PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, MainActivity.APP_TAG); wakeLock.acquire(); } public static void release() { if (wakeLock != null) wakeLock.release(); wakeLock = null; } }
/** * Used with playback state changes to correctly acquire and * release the wakelock if the user has enabled it with {@link #setWakeMode(Context, int)}. * If the {@link #wakeLock} is null then no action will be performed. * * @param awake True if the wakelock should be acquired */ protected void stayAwake(boolean awake) { if (wakeLock == null) { return; } if (awake && !wakeLock.isHeld()) { wakeLock.acquire(WAKE_LOCK_TIMEOUT); } else if (!awake && wakeLock.isHeld()) { wakeLock.release(); } }
private PowerManager.WakeLock wl; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNjfdhotDimScreen"); }//End of onCreate @Override protected void onPause() { super.onPause(); wl.release(); }//End of onPause @Override protected void onResume() { super.onResume(); wl.acquire(); }//End of onResume
@Override public void onStart() { Context appContext = getApplicationContext(); WifiManager wifiManager = (WifiManager) appContext.getSystemService(Context.WIFI_SERVICE); wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, TAG); wifiLock.acquire(); PowerManager powerManager = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE); wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); wakeLock.acquire(); super.onStart(); }
private static void acquireWakeLockAndSendMessage(Context context, Message msg, long delay) { if (mWakeLock == null) { Context appContext = context.getApplicationContext(); PowerManager pm = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Timber headset button"); mWakeLock.setReferenceCounted(false); } if (DEBUG) Log.v(TAG, "Acquiring wake lock and sending " + msg.what); // Make sure we don't indefinitely hold the wake lock under any circumstances mWakeLock.acquire(10000); mHandler.sendMessageDelayed(msg, delay); }
private static void acquireWakeLockAndSendMessage(Context context, Message msg, long delay) { if (mWakeLock == null) { Context appContext = context.getApplicationContext(); PowerManager pm = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Timber headset button"); mWakeLock.setReferenceCounted(false); } if (DEBUG) Log.v(TAG, "Acquiring wake lock and sending " + msg.what); // Make sure we don't indefinitely hold the wake lock under any circumstances mWakeLock.acquire(10000); mHandler.sendMessageDelayed(msg, delay); }