Monitor USB plug-and-Unplug in Android

Source: Internet
Author: User

First, the demand

In the Android should develop the process, the use of USB communication, need to apply monitoring USB device Plug and unplug, so as to refresh the USB device list.

Second, the realization

In use, register and unregister are required.

public class Usbbroadcastreceiver extends Broadcastreceiver {

Private Onusblistener Usblistener;

@Override
public void OnReceive (context context, Intent Intent) {
String action = Intent.getaction ();
if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals (ACTION) | |
UsbManager.ACTION_USB_DEVICE_ATTACHED.equals (ACTION)) {
Usblistener.onstatechanged ();
}
}

public void Registerusbreceiver (context context) {
Intentfilter filter = new Intentfilter ();
Filter.addaction (usbmanager.action_usb_device_attached);
Filter.addaction (usbmanager.action_usb_device_detached);
Context.registerreceiver (this, filter);
}

public void Unregisterusbreceiver (context context) {
Context.unregisterreceiver (this);
}

public void Setusblistener (Onusblistener usblistener) {
This.usblistener = Usblistener;
}

@FunctionalInterface
Public interface Onusblistener {
void Onstatechanged ();
}
}

Monitor USB plug-and-Unplug in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.