How to check if USB connected mode is enabled on android?

12,537

Solution 1

This is similar to this: Check USB Connection Status on Android

Although they do make use of Broadcast Receivers. You say this is a stupid way of doing things, but can you be more specific about that?

It is not the case that you can't detect it after the app has started. What you would need to do would be to put

<intent-filter>
<action android:name="android.intent.action.ACTION_UMS_CONNECTED"/>
<action android:name="android.intent.action.ACTION_UMS_DISCONNECTED"/>

in your AndroidManifest.xml underneath the <Receiver> entry for the following class:

public class IntentReceiver extends BroadcastReceiver {

        @Override

        public void onReceive(Context context, Intent intent) {

                // TODO Auto-generated method stub

                if(intent.getAction().equals(Intent.ACTION_UMS_CONNECTED)){

                        Toast.makeText(context, "mounted", Toast.LENGTH_LONG).show();

                        Intent myStarterIntent = new Intent(context, tst.class);

                         myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                        context.startActivity(myStarterIntent);

                }

        }

}

`

Sources Cited: http://www.anddev.org/start_activity_on_usb_connect-t8814.html

Solution 2

A1: You said "users often connect their devices to their computers to add music into the apps folder", I guess you mean that the Sd card has connected to PC in MassStorage mode, you can check this as following:

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_SHARED.equals(state)) {
    // Sd card has connected to PC in MSC mode
}

A2: You said "This won't work if the user starts the app after connecting to USB", I can't agree with you, in ICS, you can register a receiver to listen android.hardware.action.USB_STATE changes, the intent broadcasted by system is in STICKY mode, that to say even the receiver in your app is registered after usb cable connecting, you can also get this message:

Intent intent = context.registerReceiver(...);
if (intent != null) {
    boolean isConnected = intent.getBooleanExtra(USB_CONNECTED, false);
    boolean isMscConnection = intent.getBooleanExtra(USB_FUNCTION_MASS_STORAGE, false);
}

the intent returned in the method mentioned above the message you missed before usb cable connection to PC.

refer to the link below for details: http://www.androidadb.com/source/toolib-read-only/frameworks/base/core/java/android/hardware/usb/UsbManager.java.html

Share:
12,537
NJGUY
Author by

NJGUY

Updated on June 04, 2022

Comments

  • NJGUY
    NJGUY almost 2 years

    I have an app on the android market and users often connect their devices to their computers to add music into the apps folder. I have specifically stated in the instructions that android apps cannot communicate with the sd card while usb connected mode is enabled. Apparently this is not stupid proof enough. Is there a way to detect in java if the USB is connected? I have seen it done with broadcast receivers, but this is such a stupid way to do it. This won't work if the user starts the app after connecting to USB. There has got to be a better way.

  • NJGUY
    NJGUY over 12 years
    "It is not the case that you can't detect it after the app has started." I meant the opposite of what you said. You only receive the broadcast when the usb is connected or disconnected, so if you start the app in between those actions you don't have any idea if it is connected or not.