关闭android中的屏幕

时间:2017-12-07 23:52:29

标签: android screen securityexception device-policy-manager screen-off

嗨,对于我正在研究的项目,我必须关闭Android设备的屏幕。在这里搜索后,我开始使用这段代码:

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"在活动中按下按钮后运行。 " AdminReceiver.class"看起来像这样:

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");
    }
}

这似乎适用于其他人,我甚至试过一些github项目,它们的工作方式相同。但是,我继续收到以下错误消息,由" devicePolicyManager.lockNow()触发;":

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

显然我在这里做错了。任何人都可以帮我找到它吗?

PS:我的AndroidManifest.xml包含以下几行,您知道:

    <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>

提前致谢

1 个答案:

答案 0 :(得分:0)

感谢用户CommonsWare我发现res/xml/device_admin.xml文件为空:

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

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

在我看来,它应该是这样的:

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

非常感谢您的快速回答!

我应该在复制粘贴代码之前阅读developer.android.com上的Device Admin page我不太了解。下次我会