简体   繁体   中英

Turning off the screen in android

Hi, for a project I'm working on I have to turn off the screen of the android device. After searching on here I came to use this code:

public static final int REQUEST_ADMIN_ENABLE = 12345;
ComponentName dPAComponent;
DevicePolicyManager devicePolicyManager;

public void screenOff(){
    dPAComponent = new ComponentName(this, AdminReceiver.class);
    devicePolicyManager = (DevicePolicyManager)
            getApplicationContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, dPAComponent);
    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "pls gib");
    startActivityForResult(intent, REQUEST_ADMIN_ENABLE);
}

@Override
protected void onActivityResult(int request, int result, Intent data){
    super.onActivityResult(request, result, data);
    if(devicePolicyManager.isAdminActive(dPAComponent)){
        if(result == RESULT_OK) {

            switch (request) {
                case REQUEST_ADMIN_ENABLE:
                    devicePolicyManager.lockNow();
                    break;
            }
        }

    }
}

"screenOff" is run once a button is pressed in the activity. "AdminReceiver.class" looks like this:

public class AdminReceiver extends DeviceAdminReceiver {

    @Override
    public void onEnabled(Context context, Intent intent) {
        Log.d("AdminReceiver", "Device Admin enabled");
        //Common.becomeHomeActivity(context);
    }

    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
        return "Warning: Device Admin is going to be disabled.";
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
        Log.d("AdminReceiver", "Device Admin disabled");
    }

    @Override
    public void onLockTaskModeEntering(Context context, Intent intent, String pkg) {
        Log.d("AdminReceiver", "Kiosk Mode enabled");
    }

    @Override
    public void onLockTaskModeExiting(Context context, Intent intent) {
        Log.d("AdminReceiver", "Kiosk Mode disabled");
    }
}

This seems to work for others, I even tried some github projects which worked the same way. I, however, keep getting the following error message, triggered by "devicePolicyManager.lockNow();":

Caused by: java.lang.SecurityException: No active admin owned by uid 10109 for policy #3

Obviously I'm doing something wrong here. Can anyone help me find it?

PS: My AndroidManifest.xml contains the following lines, just so you know:

    <receiver
        android:name=".AdminReceiver"
        android:label="My DeviceAdmin"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/device_admin" />

        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
    </receiver>

Thanks in advance

Thanks to user CommonsWare I found out that the res/xml/device_admin.xml file was empty:

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>

    </uses-policies>
</device-admin>

In my case it should look like this:

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <force-lock />
    </uses-policies>
</device-admin>

Thanks a lot for the quick answer!

I should've read the Device Admin page on developer.android.com before copy-pasting code I didn't quite understand. Next time I will

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM