devicePolicyManager.lockNow() is not working

public final static void lockDevice()
    {
        try
        {
            if (devicePolicyManager.isAdminActive(adminComponent))
            {
                devicePolicyManager.lockNow();
            }
        }
        catch (final Exception ex)
        {
            ...
        }
    }


The above code does not throw any exception nor it locks the screen for motorola xoom tablets only. (Both Homeycomb and Icecream Sandwitch) The same code works perfectly on other Homeycomb and ICS tablets.


I googled, but did not get any solution. Any Ideas.....?

--------------------------------------------------------------------


Possible reasons for this problem


1) I think there is some problem with receiver's meta-data in your AndroidManifest.xml


2) You haven't added the correct class(extended with DeviceAdminReceiver) to either adminComponent OR to android:name property of receiver.


After spending lot of time on this i have created the code.


Code for main Activity

public class LockerTest extends Activity {
    protected static final int REQUEST_ENABLE = 0;
    DevicePolicyManager devicePolicyManager;
    ComponentName adminComponent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Button button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(btnListener);


    }


    Button.OnClickListener btnListener = new Button.OnClickListener() {
        public void onClick(View v) {
            adminComponent = new ComponentName(LockerTest.this, Darclass.class);
            devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);


            if (!devicePolicyManager.isAdminActive(adminComponent)) {


                Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent);
                startActivityForResult(intent, REQUEST_ENABLE);
            } else {
                devicePolicyManager.lockNow();
            }


        }
    };


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (REQUEST_ENABLE == requestCode) {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }


}


Create a new class - Darclass - code


import android.app.admin.DeviceAdminReceiver;


public class Darclass extends DeviceAdminReceiver{


}
Create a folder 'xml' in 'res'. Then create my_admin.xml file in 'xml' folder. Code for my_admin.xml. Note add this receiver after </activity> and before </application>

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <limit-password />
        <watch-login />
        <reset-password />
        <force-lock />
        <wipe-data />
    </uses-policies>
</device-admin>
Finally add the receiver given bellow to your AndroidManifest.xml

<receiver
            android:name=".Darclass"
            android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/my_admin" />


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

It should work on your device.



http://developer.android.com/guide/topics/admin/device-admin.html
在 Android 13 中删除图案解锁可以通过以下步骤实现: 1. 在 AndroidManifest.xml 文件中添加权限声明: ```xml <uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" /> ``` 2. 创建 DeviceAdminReceiver 类,实现对设备管理员权限的管理: ```java public class DeviceAdminReceiver extends android.app.admin.DeviceAdminReceiver { @Override public void onEnabled(Context context, Intent intent) { super.onEnabled(context, intent); } @Override public void onDisabled(Context context, Intent intent) { super.onDisabled(context, intent); } } ``` 3. 在你的 Activity 中添加以下代码: ```java DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); ComponentName componentName = new ComponentName(this, DeviceAdminReceiver.class); if (devicePolicyManager.isAdminActive(componentName)) { devicePolicyManager.setPasswordQuality(componentName, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED); devicePolicyManager.resetPassword("", DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY); devicePolicyManager.lockNow(); devicePolicyManager.removeActiveAdmin(componentName); } else { Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN); } ``` 4. 在 onActivityResult 方法中添加以下代码: ```java if (requestCode == REQUEST_CODE_ENABLE_ADMIN) { if (resultCode == RESULT_OK) { // 成功获取设备管理员权限,可以删除图案解锁了 DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); ComponentName componentName = new ComponentName(this, DeviceAdminReceiver.class); devicePolicyManager.setPasswordQuality(componentName, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED); devicePolicyManager.resetPassword("", DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY); devicePolicyManager.lockNow(); devicePolicyManager.removeActiveAdmin(componentName); } else { // 没有获取设备管理员权限,不能删除图案解锁 } } ``` 这样就可以删除图案解锁了。需要注意的是,在不需要删除图案解锁时,应该调用 `devicePolicyManager.setPasswordQuality(componentName, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING)` 来重新启用图案解锁。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值