0

DevicePolicyManager在需要时用来锁定设备。我想要的是调出锁定屏幕,但使用DevicePolicyManager和调用它的方法lockNow()将锁定设备并关闭屏幕。如何防止关闭屏幕?

更新:我试过这个:

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();
}

}

4

2 回答 2

0

我终于找到了解决这个问题的方法。因为我想在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();
}

它运作良好。锁定后打开屏幕...

于 2013-09-01T11:16:48.353 回答
0

与您的 lockNow() 一起,添加PowerManager.WakeLock

进口

import android.os.PowerManager;

全局变量

protected PowerManager.WakeLock mWakeLock;

公共方法

    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();
    }

在清单文件中使用以下权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />
于 2013-09-01T10:10:34.997 回答