Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android USB attached Status;

Tags:

java

android

I want to do the replication between SQL SERVER & SQLite.ITs wroking with WIFI netconnection.

My Problem is: Network priority USB(Connect to PC),GPRS,WIFI. This is the prioprity order. Wifi & GPRS are ok. USB is a problem. How can i identiify Android device attached to PC.

   public class DownlaodTableActivity extends Activity{
        private int networkType;
        private int signalStrengthInt;
        private SignalStrength signalStrength;
        private TelephonyManager telephonyInfo;
           static boolean usbStatus = false;

       public static BroadcastReceiver mReceiver1  = new BroadcastReceiver() {
       public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, context.toString(),Toast.LENGTH_LONG).show();
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
            Toast.makeText(context, "SD Card mounted",Toast.LENGTH_LONG).show();

        } else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) {
            Toast.makeText(context, "SD Card unmounted",Toast.LENGTH_LONG).show();

        } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)) {
            Toast.makeText(context, "SD Card scanner started",Toast.LENGTH_LONG).show();

        } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
            Toast.makeText(context, "SD Card scanner finished",Toast.LENGTH_LONG).show();

        } else if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
            Toast.makeText(context, "SD Card eject",Toast.LENGTH_LONG).show();

        } else if(action.equals(Intent.ACTION_UMS_CONNECTED)){
            Toast.makeText(context, "connected",Toast.LENGTH_LONG).show();
            usbStatus =true;
        } else if(action.equals(Intent.ACTION_UMS_DISCONNECTED)) {
            Toast.makeText(context, "disconnected",Toast.LENGTH_LONG).show();
            usbStatus =false;
        }

        if(UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(action)) {
                usbStatus =true;
        }else{
                usbStatus =false;
        }

        int level = intent.getIntExtra("level", 0);
        Toast.makeText(context, "Battery Level is :"+level+" Please plugin charger!!!",Toast.LENGTH_LONG).show();


        context.unregisterReceiver(this);
        int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        int levels = -1;
        if (rawlevel >= 0 && scale > 0) {
            levels = (rawlevel * 100) / scale;
        }
        Toast.makeText(context, "Battery Level Remaining:"+levels+" %",Toast.LENGTH_LONG).show();
    }

};
    manager = (ConnectivityManager) getSystemService(DownlaodTableActivity.CONNECTIVITY_SERVICE);
    is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
    isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();

      telephonyInfo = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
     telephonyInfo.listen(phoneState,PhoneStateListener.LISTEN_SIGNAL_STRENGTH);
     networkType = telephonyInfo.getNetworkType();

      if (usbStatus) {................}
      else if (isWifi||is3g) { ................}
    else if(networkType==TelephonyManager.NETWORK_TYPE_EDGE) {   ...........}

And My Manifest file:

     <receiver android:name=".admin.mReceiver1">
            <intent-filter>
                 <action android:name="android.intent.action.ums_connected" />
                 <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
                <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
            </intent-filter>

            <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />
            <meta-data android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"  android:resource="@xml/device_filter" />

    </receiver> 

I tried this ways also ... http://pastie.org/3019828

Please Tell me Why it didn't take USBSttaus =true; I connected Android in PC.But still it show usb device sttached status is false.

Please help out from this question.

Thanks in advance;

like image 424
Piraba Avatar asked Jul 22 '26 14:07

Piraba


1 Answers

You have mistaken in declaring your reciever in Manifest file I think.

It should be like,

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

See here for full source about to start Service on USB connect in Last Post of This Page

like image 118
MKJParekh Avatar answered Jul 25 '26 03:07

MKJParekh