Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

USB Connection notification

Tags:

android

can we receive notification if user connected there phone through USB cable.

like image 902
Ajay Singh Avatar asked Apr 02 '11 11:04

Ajay Singh


People also ask

How do I turn on USB connection notification?

Depending on your Android version, you must do either of the following: Drag down the status bar, tap Connected as <connection type> under Notifications, and select Camera (PTP). Go to Settings > Storage > Menu > USB computer connection, and select Camera (PTP).

Why is my phone saying USB connector connected and disconnected?

So conclusion: this "USB Connector Connected" and "Disconnected" message loop might not be caused by moisture, but rather a somehow damaged USB C cable. Further, the message will go away if you wait long enough for the phone to work through the message buffer.

Why does my Samsung keep saying USB connected then disconnected?

The reason it's saying connected-disconnected is because it's recognising that power is going to the phone, but not enough that it can actually charge it.

How do I change my USB settings on my Samsung?

On some devices, it might be accessible from Settings > System > Advanced. Either way, once on the Developer Options page, scroll down a bit and tap on 'Default USB Configuration.


2 Answers

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.

like image 175
JohnnyLinTW Avatar answered Oct 11 '22 10:10

JohnnyLinTW


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 :-\

like image 33
Will Tate Avatar answered Oct 11 '22 11:10

Will Tate