我想通过我的应用程序关闭屏幕,但我无法获得设备管理员。
这是我的代码:
DevicePolicyManager mDPM;
ComponentName mDeviceAdminSample;
private static final int REQUEST_CODE_ENABLE_ADMIN = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext=this;
mDeviceAdminSample = new ComponentName(this, Act_01_LockScreen.class);
mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
}
@Override
public void onResume() {
super.onResume();
Boolean mAdminActive = isActiveAdmin();
if (!mAdminActive) {
// Launch the activity to have the user enable our admin.
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
mContext.getResources().getString(R.string.app_name));
startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN);
// return false - don't update checkbox until we're really active
}else{
mDPM.lockNow();
}
}
private boolean isActiveAdmin() {
return mDPM.isAdminActive(mDeviceAdminSample);
}
}
此代码不向我显示任何内容。我该怎么做?
非常感谢......
请不要告诉我任何没有解决方案的链接......
答案 0 :(得分:1)
作为一种解决方法,您可以根据亮度级别的变化尝试此技巧。
Window window = getWindow();
WindowManager.LayoutParams windowParams = window.getAttributes();
windowParams.screenBrightness = 0;
window.setAttributes(windowParams);