When an Android phone (which should be the case for all mobile phones) receives a text message or call, the screen is automatically lit and the screen lock is unlocked for immediate operation, the following code is used to implement this function:
Keyguardmanager Km = (keyguardmanager) getsystemservice (context. keyguard_service );
// Obtain the key lock manager object
Keyguardlock KL = km. newkeyguardlock ("unlock ");
// The parameter is the tag used in logcat.
KL. disablekeyguard ();
// Unlock
Powermanager PM = (powermanager) getsystemservice (context. power_service );
// Obtain the power manager object
Powermanager. wakelock WL = PM. newwakelock (powermanager. acquire_causes_wakeup | powermanager. screen_dim_wake_lock, "bright ");
// Obtain the powermanager. wakelock object. The following parameter | indicates that two values are passed in at the same time, and finally the tag used in logcat
WL. Acquire ();
// Light up the screen
WL. Release ();
// Release
You need to add permissions in androidmanifest. xml:
12 |
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> |