博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
devicePolicyManager.lockNow() is not working
阅读量:4209 次
发布时间:2019-05-26

本文共 2870 字,大约阅读时间需要 9 分钟。

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>
Finally add the receiver given bellow to your AndroidManifest.xml
It should work on your device.

转载地址:http://qolli.baihongyu.com/

你可能感兴趣的文章
Linux上安装MySQL5.6
查看>>
Python 面向对象
查看>>
git 阿里云代码托管
查看>>
Excel 如何使某一列的值同时乘以某一个数
查看>>
【题解】长度为素数的路径个数-C++
查看>>
【题解】埃及分数-C++
查看>>
HTML基础-浏览器渲染操作的顺序
查看>>
(转)Javascript模块化编程(一):模块的写法
查看>>
解题摘要
查看>>
String系列
查看>>
Oracle 中 over() 函数
查看>>
JS 去掉回车按钮的点击事件
查看>>
PHP 登录完成后如何跳转上一访问页面
查看>>
版本控制commit和update过程
查看>>
浅析基于引用计数的字符串类string(转载)
查看>>
hadoop03---nginx+keepalived
查看>>
oc-06-无参方法的调用
查看>>
vue14 自定义过滤器
查看>>
C++类的定义之作用域
查看>>
xunsearch开发流程(三)
查看>>