Java Code Examples for android.app.admin.DevicePolicyManager#lockNow()
The following examples show how to use
android.app.admin.DevicePolicyManager#lockNow() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeyguardActivity.java From AcDisplay with GNU General Public License v2.0 | 6 votes |
/** * Locks the device (and turns screen off). * * @return {@code true} if successful, {@code false} otherwise. * @see DevicePolicyManager#lockNow() */ public boolean lock() { DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); try { // TODO: Respect secure lock timeout settings. mUnlockingTime = 0; dpm.lockNow(); return true; } catch (SecurityException e) { String errorMessage = "Failed to lock the screen due to a security exception."; ToastUtils.showLong(this, errorMessage); Log.e(TAG, errorMessage); // Clear the FLAG_KEEP_SCREEN_ON flag to prevent the situation when // AcDisplay stays forever on. Normally this should never happen. getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); return false; // Screw you owner! } }
Example 2
Source File: ScreenOffActivity.java From android-screen-off with MIT License | 6 votes |
/** * Turns the screen off and locks the device, provided that proper rights * are given. * * @param context * - The application context */ static void turnScreenOff(final Context context) { DevicePolicyManager policyManager = (DevicePolicyManager) context .getSystemService(Context.DEVICE_POLICY_SERVICE); ComponentName adminReceiver = new ComponentName(context, ScreenOffAdminReceiver.class); boolean admin = policyManager.isAdminActive(adminReceiver); if (admin) { Log.i(LOG_TAG, "Going to sleep now."); policyManager.lockNow(); } else { Log.i(LOG_TAG, "Not an admin"); Toast.makeText(context, R.string.device_admin_not_enabled, Toast.LENGTH_LONG).show(); } }
Example 3
Source File: U.java From SecondScreen with Apache License 2.0 | 6 votes |
public static void lockDevice(Context context) { if(isInNonRootMode(context)) { ComponentName component = new ComponentName(context, LockDeviceReceiver.class); context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); DevicePolicyManager mDevicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); if(mDevicePolicyManager.isAdminActive(component)) mDevicePolicyManager.lockNow(); else { Intent intent = new Intent(context, LockDeviceActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); if(context instanceof Activity) ((Activity) context).overridePendingTransition(0, 0); } } else runCommand(context, "input keyevent 26"); }
Example 4
Source File: DevicePolicyManagerUtils.java From FreezeYou with Apache License 2.0 | 5 votes |
/** * 优先 ROOT 模式锁屏,失败则尝试 免ROOT 模式锁屏 * * @param context Context */ public static void doLockScreen(Context context) { //先走ROOT,有权限的话就可以不影响SmartLock之类的了 try { Process process = Runtime.getRuntime().exec("su"); DataOutputStream outputStream = new DataOutputStream(process.getOutputStream()); outputStream.writeBytes("input keyevent KEYCODE_POWER" + "\n"); outputStream.writeBytes("exit\n"); outputStream.flush(); process.waitFor(); ProcessUtils.destroyProcess(outputStream, process); } catch (Exception e) { e.printStackTrace(); } PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (pm == null || pm.isScreenOn()) { DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); ComponentName componentName = new ComponentName(context, DeviceAdminReceiver.class); if (devicePolicyManager != null) { if (devicePolicyManager.isAdminActive(componentName)) { devicePolicyManager.lockNow(); } else { openDevicePolicyManager(context); } } else { showToast(context, R.string.devicePolicyManagerNotFound); } } }
Example 5
Source File: DeviceAdminReceiverLock.java From libcommon with Apache License 2.0 | 5 votes |
/** * スクリーンロックを行う * @return スクリーンロックできればtrue */ private static boolean checkScreenLock(@NonNull final Activity activity, final boolean finish) { final ComponentName cn = new ComponentName(activity, DeviceAdminReceiverLock.class); final DevicePolicyManager dpm = ContextUtils.requireSystemService(activity, DevicePolicyManager.class); if (dpm.isAdminActive(cn)){ // デバイス管理者が有効ならスクリーンをロック dpm.lockNow(); if (finish) { activity.finish(); } return true; } return false; }
Example 6
Source File: FloatingBallUtils.java From RelaxFinger with GNU General Public License v2.0 | 5 votes |
public static void lockScreen(){ DevicePolicyManager policyManager = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE); ComponentName adminReceiver = new ComponentName(context, ScreenOffAdminReceiver.class); boolean admin = policyManager.isAdminActive(adminReceiver); if (admin) { policyManager.lockNow(); } }
Example 7
Source File: MainActivity.java From AcDisplay with GNU General Public License v2.0 | 5 votes |
/** * Turns screen off and sends a test notification. * * @param cheat {@code true} if it simply starts {@link AcDisplayActivity}, * {@code false} if it turns device off and then uses notification * to wake it up. */ private void startAcDisplayTest(boolean cheat) { if (cheat) { startActivity(new Intent(this, AcDisplayActivity.class)); NotificationHelper.sendNotification(this, App.ID_NOTIFY_TEST); return; } int delay = getResources().getInteger(R.integer.config_test_notification_delay); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Test notification."); wakeLock.acquire(delay); try { // Go sleep DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); dpm.lockNow(); new Handler().postDelayed(new Runnable() { private final Context context = getApplicationContext(); @Override public void run() { NotificationHelper.sendNotification(context, App.ID_NOTIFY_TEST); } }, delay); } catch (SecurityException e) { Log.wtf(TAG, "Failed to turn screen off!"); wakeLock.release(); } }
Example 8
Source File: LocAlarmService.java From ownmdm with GNU General Public License v2.0 | 5 votes |
/** * lock */ private void lock() { Util.logDebug("lock()"); DevicePolicyManager mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); ComponentName mDeviceAdmin = new ComponentName(this, MdmDeviceAdminReceiver.class); if (mDPM.isAdminActive(mDeviceAdmin)) { mDPM.lockNow(); } }
Example 9
Source File: OBSystemsManager.java From GLEXP-Team-onebillion with Apache License 2.0 | 4 votes |
public void screenLock() { DevicePolicyManager devicePolicyManager = (DevicePolicyManager) MainActivity.mainActivity.getSystemService(Context.DEVICE_POLICY_SERVICE); devicePolicyManager.lockNow(); }
Example 10
Source File: URMUtils.java From rebootmenu with GNU General Public License v3.0 | 4 votes |
/** * 用辅助功能锁屏 * * @param activity 1 * @param componentName 2 * @param requestCode 3 * @param devicePolicyManager 4 * @param needConfig 是否需要 配置管理员 */ public static void lockScreen(@NonNull Activity activity, ComponentName componentName, int requestCode, DevicePolicyManager devicePolicyManager, boolean needConfig) { new DebugLog("lockScreen", DebugLog.LogLevel.V); //设备管理器是否启用 boolean active = devicePolicyManager.isAdminActive(componentName); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { //自动移除不必要的管理员,避免在卸载时造成困扰 if (active) devicePolicyManager.removeActiveAdmin(componentName); //请求打开辅助服务设置 if (!isAccessibilitySettingsOn(activity.getApplicationContext())) { new TextToast(activity.getApplicationContext(), activity.getString(R.string.service_disabled)); try { activity.startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)); } catch (Throwable t) { t.printStackTrace(); UIUtils.visibleHint(activity, R.string.accessibility_settings_not_found); } } else LocalBroadcastManager.getInstance(activity).sendBroadcast(new Intent(UnRootAccessibility.LOCK_SCREEN_ACTION)); activity.finish(); } else { if (!active) { //请求启用 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, activity.getString(R.string.service_explanation)); try { activity.startActivityForResult(intent, requestCode); } catch (ActivityNotFoundException e) { UIUtils.visibleHint(activity, R.string.hint_add_dev_admin_activity_not_found); } } else { devicePolicyManager.lockNow(); if (needConfig) //如果需要二次确认,禁用设备管理器。(这里的策略和root模式的锁屏无需确认不同) if (!ConfigManager.get(ConfigManager.NO_NEED_TO_CONFIRM)) { devicePolicyManager.removeActiveAdmin(componentName); } activity.finish(); } } }