我正在使用此Reference处理手机锁。在我的三星标签2上运行良好。但是当我在Nexus 2上运行相同的代码时它不起作用。我在下面发布了一些代码。
它记录"DeviceAdminSample" "Admin enable FAILED!"
。任何帮助将不胜感激 !!
对Nexus有任何限制吗?
onCreate()
{
deviceManger = (DevicePolicyManager)getSystemService(
Context.DEVICE_POLICY_SERVICE);
activityManager = (ActivityManager)getSystemService(
Context.ACTIVITY_SERVICE);
compName = new ComponentName(this, MyAdmin.class);
}
private void EnableSetting() {
// TODO Auto-generated method stub
Intent intent = new Intent(DevicePolicyManager
.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
compName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"Additional text explaining why this needs to be added.");
startActivityForResult(intent, RESULT_ENABLE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RESULT_ENABLE:
if (resultCode == Activity.RESULT_OK) {
Log.i("DeviceAdminSample", "Admin enabled!");
} else {
Log.i("DeviceAdminSample", "Admin enable FAILED!");
}
}
感谢。
答案 0 :(得分:3)
主要活动代码
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的代码。注意在
之前和之后添加此接收器<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>
它应该适用于您的设备。
答案 1 :(得分:1)
我有同样的问题。这是因为我的...块在AndroidManifest.xml中不在我的块之外。当我移动...块时工作得很好,如下所示:
<activity
android:name=".Main"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyDeviceAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin" android:resource="@xml/device_admin_xml" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>