Is it possible to determine whether an Android device is plugged into a computer or just power?

前端 未结 3 1112
野性不改
野性不改 2020-12-19 20:46

I was wondering if it was possible to specifically determine if an Android device was plugged into a computer or just power.

The main reason is I\'d like for my phon

3条回答
  •  无人及你
    2020-12-19 21:14

    I actually found the exact way to choose whether to keep the screen on when plugged into AC, USB or either by looking into the source at http://google.com/codesearch#409TP6F96yI/src/com/android/settings/DevelopmentSettings.java&l=95

    Requires permission: android.permission.WRITE_SETTINGS

    Settings.System.putInt(getContentResolver(), Settings.System.STAY_ON_WHILE_PLUGGED_IN,
                    mKeepScreenOn.isChecked() ?
                    (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB) : 0);
    

    For AC: BatteryManager.BATTERY_PLUGGED_AC

    For USB: BatteryManager.BATTERY_PLUGGED_USB

    For Either: (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB)

提交回复
热议问题