Reputation: 1621
can we receive notification if user connected there phone through USB cable.
Upvotes: 5
Views: 10640
Reputation: 580
A number of years later... this might help someone that came looking like I did. Since Honeycomb, there are two actions one can listen for USB_DEVICE_ATTACHED
and USB_DEVICE_DETACHED
.
Upvotes: 0
Reputation: 176
Actually there is one broadcast event; if you turned on Debug in your application settings, your will see a bug on your notification bar when you plugged usb. Following is the sample how it works;
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// UsbManager.ACTION_USB_STATE -> "android.hardware.usb.action.USB_STATE" actually
if (action.equals(UsbManager.ACTION_USB_STATE)) {
Bundle extras = intent.getExtras();
// UsbManager.USB_CONNECTED -> "connected" actually
usbConnected = extras.getBoolean(UsbManager.USB_CONNECTED);
...
You can find this at framework/base/service/java/com/android/server/NotificationManagerService.java. Hope this helps.
Upvotes: 5
Reputation: 1084
Since: API Level 5 An activity to run when device is inserted into a car dock. Used with ACTION_MAIN to launch an activity. For more information, see UiModeManager. Constant Value: "android.intent.category.CAR_DOCK" public static final String CATEGORY_CAR_MODE
Since: API Level 8 Used to indicate that the activity can be used in a car environment. Constant Value: "android.intent.category.CAR_MODE"
Upvotes: 1
Reputation: 33509
Ajay,
I wasn't able to find anything specific to just "USB Connected," but there are a few Broadcast Actions that may be of interest in this case depending on what you are trying to accomplish:
ACTION_MEDIA_SHARED
: External media is unmounted because it is being shared via USB mass storage.ACTION_UMS_CONNECTED
: The device has entered USB Mass Storage mode. This is used mainly for the USB Settings panel.ACTION_UMS_DISCONNECTED
: The device has exited USB Mass Storage mode. This is used mainly for the USB Settings panel.There doesn't seem to be a Broadcast Action specific to USB simply being plugged in, you could also try doing something with:
ACTION_POWER_CONNECTED
: External power has been connected to the device.But this would go off for both USB connected to a computer and USB connect ONLY to a power source...
Interestingly, I also found this LINK simply stating that there was no Broadcast Action for "USB Connected".
You may be out of luck in this case :-\
Upvotes: 4