简体   繁体   English

如何锁定设备,但防止屏幕在android中关闭?

[英]how to lock device but prevent the screen to be turned off in android?

I'm using DevicePolicyManager to lock the device while needed. 我在需要时使用DevicePolicyManager锁定设备。 what I want is to bring up the lock screen but using DevicePolicyManager and calling it's method lockNow() will lock the device and turn the screen off. 我想要的是显示锁定屏幕,但是使用DevicePolicyManager并调用它的方法lockNow()将锁定设备并关闭屏幕。 how to prevent turning off the screen? 如何防止关闭屏幕?

UPDATE: I have tried this: 更新:我已经尝试过:

public class LockMeNowActivity extends Activity {
protected PowerManager.WakeLock mWakeLock;
private DevicePolicyManager mgr = null;
private ComponentName cn = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(com.commonsware.android.lockme.R.layout.main);
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
    cn = new ComponentName(this, AdminReceiver.class);
    mgr = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
}

public void lockMeNow(View v) {
    if (mgr.isAdminActive(cn)) {

        mgr.lockNow();
    } else {
        Intent intent = new Intent(
                DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                "For experimentation purposes only");
        startActivity(intent);
    }
}

public void pullWakeLock() {
    mWakeLock.acquire();
}

public void onPause() {
    super.onPause();
    pullWakeLock();
}

public void onResume() {
    super.onResume();
    releaseWakeLock();
}

public void releaseWakeLock() {
    mWakeLock.release();
}

} }

Along with your lockNow(), add PowerManager.WakeLock : 与您的lockNow()一起添加PowerManager.WakeLock

Import 进口

import android.os.PowerManager;

Global Variable 全局变量

protected PowerManager.WakeLock mWakeLock;

Public Methods 公开方法

    public void pullWakeLock() {
        final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
        this.mWakeLock.acquire();
    }

    public void releaseWakeLock() {
        this.mWakeLock.release();
    }

Use the follwing permission in manifest file : 在清单文件中使用以下权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />

I finally found a way for this problem. 我终于找到了解决这个问题的方法。 as I want to lock the device when onStop() happens I did: 因为我想在onStop()发生时锁定设备,所以我这样做了:

    public void onStop() {
    mgr.lockNow();
    super.onStop();
    WakeLock screenLock = ((PowerManager) getSystemService(POWER_SERVICE))
            .newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
                    | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
    screenLock.acquire();
}

and it worked well. 而且效果很好。 turning on the screen after locking it... 锁定屏幕后打开屏幕...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使在Android中关闭了屏幕,如何保持应用状态? - How to keep app status even if screen is turned off in Android? 设备处于睡眠状态(屏幕关闭)时,Android获取GPS坐标更新 - Android getting GPS coordinate updates while device is in sleep (Screen is turned off) 设备处于锁定状态且屏幕处于关闭状态时,如何运行我的应用程序? - How can I run my application when device is in locked state and screen is in turned off state? 如果应用程序在后台运行,如何防止屏幕锁定 - How to prevent screen lock if app is running in background 关闭/锁定屏幕-不启动活动-Android - Turn Off/Lock Screen - Without Launching the Activity - Android 屏幕关闭时,OnPause返回NullPointerException - OnPause return NullPointerException when screen is turned off JavaFX-屏幕关闭时的绘图组件 - JavaFX - Drawing component when the screen is turned off 当应用程序停止/设备关闭时,在 android 上保存片段状态/数据 - save fragment state/data on android when app stopped/device turned off 如何在锁屏 android 9.1 荣誉移动设备上检测电源按钮事件 - how to detect power button event at lock screen android 9.1 honor mobile device 设备打开时如何创建设置为默认的Android应用 - How to create an android app that is set to default when the device is turned on
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM