我最近创建了一个锁屏小部件应用程序。它总是有例外:
( java.lang.SecurityException: No active admin owned by uid 10034 for policy #3)
我尝试了很多方法,都无法消除异常。当条件成立时,我打开活动来实现锁屏的目的。
public class tempActivity extends Activity {
private static final String TAG = "tempActivity";
private DevicePolicyManager policyManager;
private ComponentName componentName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.temp);
Log.e(TAG, "Activity created~");
policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
componentName = new ComponentName(this, AdminReceiver.class);
mylock();
}
private void mylock() {
boolean active = policyManager.isAdminActive(componentName);
if (!active) { // Without permission
Log.e(TAG, "No authority~");
activeManage(); // To get access
policyManager.lockNow(); // And lock screen
} else {
Log.e(TAG, "Has authority");
policyManager.lockNow(); // lock screen directly
}
this.finish();
}
private void activeManage() {
Log.e(TAG, "activeManage");
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "developers:liushuaikobe");
startActivityForResult(intent, 1);
}
}