To open and close the phone screen method:
1. Turn off the screen
Equipment Manager
Private Devicepolicymanager Mdevicepolicymanager;
Off components
Private ComponentName Mcompname;
OnCreate ()
{
Mdevicepolicymanager = (Devicepolicymanager) getsystemservice (Context.device_policy_service);
Request permission
Mcompname = new ComponentName (this, ynadminreceiver.class);
}
Click the button to close the screen
public void Onscreenoff (view view) {
Determine if the component has system administrator privileges
if (!mdevicepolicymanager.isadminactive (mcompname)) {//This sentence must have ...
Intent Intent = new Intent ();
Specify an action
Intent.setaction (devicepolicymanager.action_add_device_admin);
Assign to that component to authorize
Intent.putextra (Devicepolicymanager.extra_device_admin, mcompname);
StartActivity (Intent);
} else {
Turn off the screen now
Mdevicepolicymanager.locknow ();
Devicepolicymanager.resetpassword ("123321", 0);
LOG.I (TAG, "have permission to lock screen ....");
LOG.I (TAG, "going to shutdown");
}
}
wherein, the component Mcompname is a deviceadminreceiver:
Package com.yn.receivers;
public class Ynadminreceiver extends Deviceadminreceiver {
}
The above is the code section of off, the remaining configuration:
Res/xml/yndeviceadmin.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<device-admin xmlns:android= "Http://schemas.android.com/apk/res/android" >
<uses-policies>
<force-lock/>
</uses-policies>
</device-admin>
Androidmainfest.xml
<!--Configure permissions--
<uses-permission android:name= "Android.permission.USES_POLICY_FORCE_LOCK"/>
<!--declaring the off-screen component--
<receiver android:name= "Com.yn.receivers.YNAdminReceiver" >
<meta-data android:name= "Android.app.device_admin"
<!--Specify component configuration--
Android:resource= "@xml/yndeviceadmin/>
<intent-filter >
<action android:name= "Android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
With the above configuration, it is possible to turn off the screen.
2. Open the screen (open the screen, just a wakelock)
Private PowerManager Mpowermanager;
Private Powermanager.wakelock Mscreenlock;
OnCreate ()
{
Mpowermanager = ((PowerManager) Getsystemservice (Power_service));
Mscreenlock = Mpowermanager.newwakelock (
Powermanager.acquire_causes_wakeup//This flag enables the screen to light up when the screen is turned off (usually the wakelock only keeps the screen turned on and is not automatically lit if the screen is extinguished)
| Powermanager.screen_dim_wake_lock
| Powermanager.on_after_release, "Screenonwakelock");
}
Click the button to get the lock after 5s (if the screen is turned off, the screen will be lit when the lock is acquired after 5s)
public void Onscreenon (view view) {
New Handler (). postdelayed (New Runnable () {
@Override
public void Run () {
Isheld (), determines whether to acquire the lock, false does not acquire the lock
if (!mscreenlock.isheld ()) {
Mscreenlock.acquire ();
Toast.maketext (Mainactivity.this, "acquire", Toast.length_short). Show ();
} else {
Mscreenlock.release ();
Toast.maketext (Mainactivity.this, "release", Toast.length_short). Show ();
}
}
}, 5000);
}
Finally, you also need to configure permissions in Androidmanifest.xml
<uses-permission android:name= "Android.permission.WAKE_LOCK"/>
Through the above steps, you can realize the screen is open.
This article is from the "whatever957" blog, make sure to keep this source http://whatever957.blog.51cto.com/6835003/1864346
Android Open off Screen