这个问题的可能原因
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>之前
最后将下面给出的接收器添加到AndroidManifest.xml中
android:name=".Darclass"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
android:name="android.app.device_admin"
android:resource="@xml/my_admin" />
它应该适用于您的设备.