Android can turn off and on the screen through broadcasting

Posted by djetaine on Fri, 08 May 2020 17:59:12 +0200

method:

PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
/*        PARTIAL_WAKE_LOCK:Keep the CPU running and the screen and keyboard lights may be off.
        SCREEN_DIM_WAKE_LOCK: Keep CPU running, allow to keep screen display but possibly gray, allow to turn off keyboard light
        SCREEN_BRIGHT_WAKE_LOCK: Keep the CPU running, allow the screen to be highlighted, and allow the keyboard light to be turned off
        FULL_WAKE_LOCK: Keep the CPU running, keep the screen highlighted, and keep the keyboard light bright
        ACQUIRE_CAUSES_WAKEUP: Force the screen to light, this lock is mainly for some operations that must inform the user
                ON_AFTER_RELEASE: Keep the screen on for a while when the lock is released*/
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK  | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE , "matrix");
wakeLock.acquire();
//wakeLock.acquire(10000);
wakeLock.release();

1. To disable and wake up the screen, you need to add the following permissions to the Manifest file:

<!-- Screen wake up -->  
<uses-permission android:name="android.permission.WAKE_LOCK" />  
<!-- Screen off -->  
<user-permission android:name="android.permission.DEVICE_POWER" /> 

2. Several main places

import android.content.Context;  
import android.os.PowerManager;  
import android.os.SystemClock;

    PowerManager pm;  
    PowerManager.WakeLock wakeLock = null;

onCreate :

pm = (PowerManager) getApplication().getSystemService(Context.POWER_SERVICE);  
wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP  
                | PowerManager.SCREEN_DIM_WAKE_LOCK, "matrix"); 

Off screen and on screen:

/** 
* Bright screen 
*/  
public void screenOn() {  
    wakeLock.acquire();  
    Log.w(TAG, "screenOn");  
}  

/** 
* Release and screen out directly 
*/  
public void screenOff() { 
    wakeLock.release();  
    pm.goToSleep(SystemClock.uptimeMillis());  
    Log.w(TAG, "screenOff");  
}  

3. Receiving broadcast is used to control the wake-up and off of the screen, so that other apps can also control the screen through broadcast.

private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {

    String SCREEN_MODE = "on";
    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if (action.equals("com.screen.ext.on")) {
            String screen_state = intent.getStringExtra("screen_state");

        Log.d(TAG, "com.screen.ext.on screen_state = "  + screen_state);
            if(SCREEN_MODE.equals(screen_state)){
                screenOn();
            }
            else {
                screenOff();
            }
        }
    }
}

4. Send broadcast control

sendBroadcast(new Intent("com.screen.ext.on").putExtra("screen_state", "on"));

sendBroadcast(new Intent("com.screen.ext.on").putExtra("screen_state", "off"));

One final note:
wakeLock.acquire() and wakeLock.release() are best paired, because android system will not enter sleep state if it detects that wakeLock acquire has not been released.

Topics: Android