How to stop the phone from charging via USB programmatically

回眸只為那壹抹淺笑 提交于 2019-12-04 10:23:16

问题


I've tried to do as much research as possible but can't find an answer to this fairly simple question (want to figure this out before I'm going to set up the SDK and everything).I'm thinking about developing my first app and am wondering whether the BATTERY_STATUS_CHARGING from the BatteryManager contains only a get function or also a set function.

I wan't to make an app in which I can manually stop the phone from charging without unplugging it from the charger (via USB) and so am wondering whether this is actually possible.


回答1:


No, Its not possible on unrooted devices. It uses Settings.System with ADB_ENABLED, but ADB_ENABLED is a Secure-System-Setting which can not be changed by a application.

Yes, It can be done on rooted phone.

There is need to add an permission android.permission.WRITE_SECURE_SETTINGS.

Same type of application on Google Play : https://play.google.com/store/apps/details?id=com.jim2&hl=en




回答2:


You can hardwire it: 12 ohms in serial still makes the phone be recognized by the computer, yet slowly discharge. See attached Picture.




回答3:


I know this thread is old but posting my ans anyways for someone reaching here later.. Try this:-
echo 0 > /sys/class/power_supply/batterycharging_enabled/charging_enabled




回答4:


This is unfortunately not possible. Android only have read APIs for battery data. ADB_ENABLED will not help in this instance either as it's only to enable debugging over USB.

Battery charging control is internal to the Android kernel and battery IC modules. Manufacturers do change it from time to time, but it's not available to apps without root and without controlling the IC (integrated chip) modules through some tricky interface which would actually be different for almost every device out there!

At best if we're lucky, a sysfs interface might help do that, but again it requires root and may differ from device to device.




回答5:


Use the below command to stop the charging of mobile/handheld when connected to USB:

adb shell dumpsys battery set ac 0; dumpsys battery set usb 0;



来源:https://stackoverflow.com/questions/15993635/how-to-stop-the-phone-from-charging-via-usb-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!