Search code examples
javaandroidandroid-intentandroid-studiobroadcastreceiver

Turning Sleeping Android Device on in BroadcastReceiver Class


I have a broadcastreceiver class that starts a service. What I want to happen is when it starts the service it should also turn the device screen on. I haven't found a solution to implement. I tried extending the WakefulBroadcastReceiver but it won't open the screen, rather it keeps the device on if it was already on. Does anyone know of a solution?


Solution

  • try this method

     public void turnScreenOn(Context context) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        @SuppressWarnings("deprecation")
        WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
        wakeLock.acquire();
        wakeLock.release();
    }
    

    requires the permission

      <uses-permission android:name="android.permission.WAKE_LOCK"/>