devicePolicyManager.lockNow() is not working

本文提供了解决Android设备管理器权限激活失败和屏幕无法锁定的问题的步骤,包括创建设备管理员接收者、配置权限和元数据,以及在主活动中实现设备管理功能的完整代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值