试图将Android设备上睡觉,但电源管理不包含" goToSleep(long)方法 [英] Attempting to put Android device to sleep, but the PowerManager does not contain a "goToSleep(long) method

查看:2792
本文介绍了试图将Android设备上睡觉,但电源管理不包含" goToSleep(long)方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把一个装置睡觉,我已经找到<一个href=\"http://stackoverflow.com/questions/9734353/hwo-to-set-android-to-standby-programmatically\">references各地有关使用电源管理类的goToSleep(long)方法,但我没有看到它在文档中,当我尝试它不工作在我的code。使用它。

Android的文档不包含goToSleep方法,我可以看到

我的code:

 私人无效sleepDevice(){
    尝试{
        电源管理powerMgr =(电源管理)getSystemService(Context.POWER_SERVICE);
        时间长= 1000;
        powerMgr.goToSleep(时间);
    }赶上(例外前){
        updateStatus(错误尝试重新启动设备。);
        updateStatus(ex.getLocalizedMessage());
    }
}

Android的工作室不让code与消息编译,无法解析法'goToSleep(长)。

我甚至不看此方法,因为pcated德$ P $。另外,我并不需要担心安全权限,呼叫打算在扎根设备上运行或无根设备优雅失败。<​​/ P>

解决方案

您可以使用DeviceAdministrator,但你需要的用户授予的权利。

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.

解决方案

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

这篇关于试图将Android设备上睡觉,但电源管理不包含&QUOT; goToSleep(long)方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆