android – devicePolicyManager.lockNow()不适用于Motorola Tablets
作者:互联网
public final static void lockDevice()
{
try
{
if (devicePolicyManager.isAdminActive(adminComponent))
{
devicePolicyManager.lockNow();
}
}
catch (final Exception ex)
{
...
}
}
上面的代码不会抛出任何异常,也不会锁定motorola xoom平板电脑的屏幕. (Homeycomb和Icecream Sandwitch)相同的代码在其他Homeycomb和ICS平板电脑上完美运行.
我用Google搜索,但没有得到任何解决方案.有任何想法吗…..?
解决方法:
这个问题的可能原因
1)我认为AndroidManifest.xml中接收者的元数据存在一些问题
2)您没有将正确的类(使用DeviceAdminReceiver扩展)添加到adminComponent或者接收者的android:name属性.
花了很多时间在这之后我创建了代码.
主要活动代码
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);
}
}
}
创建一个新类 – Darclass – 代码
import android.app.admin.DeviceAdminReceiver;
public class Darclass extends DeviceAdminReceiver{
}
在’res’中创建一个’xml’文件夹.然后在’xml’文件夹中创建my_admin.xml文件. my_admin.xml的代码.注意在< / activity>之后添加此接收器在< / 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>
最后将下面给出的接收器添加到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>
它应该适用于您的设备.
标签:android,locking,block-device,motorola,device-policy-manager 来源: https://codeday.me/bug/20190928/1828704.html