插入充电器时如何保持屏幕关闭

时间:2016-02-16 20:31:06

标签: java android batterymanager

我注意到,当我们将Android设备连接到充电器时,屏幕会唤醒 所以我的问题是 每次我们将设备连接到充电器时,是否可以制作限制屏幕启用的应用程序。

我尝试使用代码,但似乎无法正常工作

    int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
    boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;


    if(usbCharge|acCharge)
    {
        turnScreenOFF();
    }
}
private void turnScreenOFF(){
    params = getWindow().getAttributes();
    /** Turn OFF: */
   /* params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
    params.screenBrightness = 0.0f; // 0.1f to turn the brightness to lowest
    getWindow().setAttributes(params);*/
    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.screenBrightness = -1;
    getWindow().setAttributes(params);
}

0 个答案:

没有答案