Detect USB connection

5

.

This is a sample program to show the broadcast notification that USB connected and disconnected. Last topic published in this forum is Location Manager

Underlying Algorithm:

Basic description of algorithm in step by step form:
1.) Create a Project DetactUSB.
2.) Create a class DetactUSB.java, which will extends BroadcastReceiver.
3.) Put the following code in DetactUSB.java:

package com.app.DetactUSB;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

public class DetactUSB extends BroadcastReceiver
{ 
    private static final String TAG = "DetactUSB";
    @Override
    public void onReceive(Context context, Intent intent) {
	// TODO Auto-generated method stub
	if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_CONNECTED"))
	{
		TextView textView = new TextView(context);
		textView.setBackgroundColor(Color.MAGENTA);
		textView.setTextColor(Color.BLUE);
		textView.setPadding(10,10,10,10);
		textView.setText("USB connected..........");
		Toast toastView = new Toast(context);
                toastView.setDuration(Toast.LENGTH_LONG);
		toastView.setGravity(Gravity.CENTER, 0,0);
		toastView.setView(textView);
                toastView.show();
		Log.i(TAG,"USB connected..");
	}

	if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_DISCONNECTED"))
	{
		TextView textView = new TextView(context);
		textView.setBackgroundColor(Color.MAGENTA);
		textView.setTextColor(Color.BLUE);
		textView.setPadding(10,10,10,10);
		textView.setText("USB Disconnected..........");
		Toast toastView = new Toast(context);
                toastView.setDuration(Toast.LENGTH_LONG);
		toastView.setGravity(Gravity.CENTER, 0,0);
		toastView.setView(textView);
                toastView.show();
	}
    } 
}

4.) Add receiver in AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.app.DetactUSB"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.INTERNET">
    </uses-permission>
    <uses-permission android:name="android.permission.RECORD_AUDIO">
    </uses-permission>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
       <activity android:name=".MyActivity" android:label="@string/app_name">
		<intent-filter>
		     <action android:name="android.intent.action.MAIN" />
		     <category android:name="android.intent.category.LAUNCHER" />
		</intent-filter>
	</activity>
    <receiver android:name=".DetactUSB">
       <intent-filter>
            <action android:name="android.intent.action.UMS_CONNECTED" />
            <action android:name="android.intent.action.UMS_DISCONNECTED" />
       </intent-filter>
    </receiver>
    </application>
</manifest>

5.) To detect USB :

android.intent.action.UMS_CONNECTED
android.intent.action.UMS_DISCONNECTED

6.) Run the application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. DetactUSB. Enter following information:
Project name: DetactUSB
Build Target: Android 2.1
Application name: DetactUSB
Package name: com.app.DetactUSB
Create Activity: MyActivity

On Clicking Finish DetactUSB code structure is generated with the necessary Android Packages being imported along with MyActivity.java. MyActivity class will look like following:

package com.app.DetactUSB;

import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity 
{
	/** Called when the activity is first created. */
	@Override
        public void onCreate(Bundle savedInstanceState) 
	{
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
	}
}

Output : The final output will look like following :

  • Kasi

    i am fully confused with this code… How the activity changes from MyActivity.java to DetactUSB.java…. and also we need to add MyActivity.java to android manifest file…. i have done all these… but it is still showing an error “the application has stopped unexpectdely” plz help on this topic….

  • http://edumobile.org Sushant

    When you set a receiver in your activity (DetactUSB for above example) and define filters in it as we did

    <receiver android:name=".DetactUSB">
           <intent-filter>
                <action android:name="android.intent.action.UMS_CONNECTED" />
                <action android:name="android.intent.action.UMS_DISCONNECTED" />
           </intent-filter>
    </receiver>
    

    on launching your app your main activity (MyActivity in this case) will call that receiver internally on receiving defined intents or actions. This is how and why activity changes from MyActivity.java to DetactUSB.java.

    There is no need to add manually MyActivity.java into your manifest as it gets added automatically when you create project.

     <activity android:name=".MyActivity" android:label="@string/app_name">
                    <intent-filter>
                         <action android:name="android.intent.action.MAIN" />
                         <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
     </activity>    
     

    If you still getting some force close check you manifest permissions and send me the “logcat” results.

    Thanks.

  • Tuyen

    please help to me, I copy the same code, but not run, i don’t know why, my android 3.2

  • Hannes

    Seems UMS_CONNECTED is deprecated. I tried MEDIA_MOUNTED but cant seem to get it to work. Any suggestions. Thanks

  • mudit srivastava

    this code is not working in 2.3.3 as the “usb connected” is not displaying on the screen. please help me to solve the error.

Proudly designed by Theme Junkie.