Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

java - Attempting to put Android device to sleep, but the PowerManager does not contain a "goToSleep(long) method

I am attempting to put a device to sleep and I have found references all over about using the PowerManager class's goToSleep(long) method but I do not see it in the documentation and it does not work when I attempt to using it in my code.

Android's documentation does not contain a goToSleep method that I could see.

My Code:

 private void sleepDevice() {
    try {
        PowerManager powerMgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
        long time = 1000;
        powerMgr.goToSleep(time);
    } catch (Exception ex) {
        updateStatus("Error attempting to reboot device.");
        updateStatus(ex.getLocalizedMessage());
    }
}

Android Studio does not let the code compile with the message, "Cannot resolve method 'goToSleep(long).

I don't even see this method as deprecated. Also, I don't need to worry about security permissions, the call is intended to run on rooted devices or fail elegantly on non-rooted devices.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the DeviceAdministrator , but you need the user to grant you those rights.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...