Say you had a device that requires 5VDC to operate. It can actually run safely at about 4.8VDC. It normally runs off of USB power but I would like to rig it up to run off of a battery. Would it be possible to design a circuit that would allow the device to run and charge the battery when connected to a usb port and then when unplugged, run off battery power?
Yes, this is possible, however from a USB 2.0 port, you can actually only draw 100mA without requesting extra power from the host. In order to actually get up to the 500mA limit you have to inform the host that you need the extra power during the USB enumeration. See this post:
It would be possible if the device which is the load consumes less current than the total current supplied from the USB port. So lets taken an example. A regular powered USB 2.0 port can deliver a maximum of 500mA at 5V to your device. If the device itself draws a total of 170 mA then there is a reserve capacity of:
500 - 170 = 330mA
This 330mA would be available for use to charge the battery minus any current that would be consumed by extra circuitry used to manage the battery charger and the battery usage mode selector.