devicePolicyManager.lockNow() is not working

本文提供了解决Android设备管理器权限激活失败和屏幕无法锁定的问题的步骤,包括创建设备管理员接收者、配置权限和元数据,以及在主活动中实现设备管理功能的完整代码示例。
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
Log.e(TAG, "-----LOCK_SCREEN-----"); mDeviceAdminSample = new ComponentName(mContext, LockReceiver.class); mDPM = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); Log.e(TAG, "mDeviceAdminSample: " + mDeviceAdminSample); Log.e(TAG, "mDPM: " + mDPM); Log.e(TAG, "mDPM.isAdminActive(mDeviceAdminSample): " + mDPM.isAdminActive(mDeviceAdminSample)); if (mDPM.isAdminActive(mDeviceAdminSample)) { Log.e(TAG, "-----LOCK_SCREEN_YES-----"); try { // Android 7.0+ 需检查密码复杂度要求 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 验证密码是否符合策略 if (!mDPM.isActivePasswordSufficient()) { Log.w(TAG, "Password does not meet requirements"); } } // 先设置密码再锁屏(顺序很重要) boolean resetSuccess = mDPM.resetPassword("111111", DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY); Log.e(TAG, "Password reset result: " + resetSuccess); mDPM.lockNow(); // 立即锁屏 } catch (SecurityException e) { Log.e(TAG, "Security exception: " + e.getMessage()); } } else { Log.e(TAG, "-----LOCK_SCREEN_NO-----"); Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "需要激活管理员权限以启用安全锁屏"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mContext.startActivity(intent); // 启动设备管理员激活界面 } catch (ActivityNotFoundException e) { Log.e(TAG, "Admin activity not found: " + e.getMessage()); } } mDPM.isAdminActive(mDeviceAdminSample) 怎么才能true
07-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值