2011-12-15 177 views
3

我想执行SQL SERVER & SQLite.ITs与WIFI网络连接之间的复制。Android USB连接状态;

我的问题是:网络优先级USB(连接到PC),GPRS,WIFI。这是保证令。 Wifi & GPRS还行。 USB是一个问题。我如何识别连接到PC的Android设备?

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) { ...........} 

和我的清单文件:

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

我想这也是办法... http://pastie.org/3019828

请告诉我,为什么它没有考虑USBSttaus = TRUE; 我在PC上连接Android,但仍显示USB设备的连接状态为false。

请帮忙解决这个问题。

在此先感谢;

+0

它更重要的是如何将你传送过来的数据USB to PC – ingsaurabh 2011-12-15 05:05:23

+0

我真的不知道我要如何进行preocess。在我的本地PC上运行它的服务。然后它会自动下载数据。 – Piraba 2011-12-15 05:30:08

回答

2

你错误地声明你的接收者在我认为的Manifest文件中。

应该是这样,

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

在这里看到完整的源即将于USB启动服务在最后发表连接的This Page

2

我会坚持让你创建一个内部类或创建BroadcastReceiver的单独班级。您似乎已在Manifest文件中声明了BroadcastReceiver。所以,如果你有BroadcastReceiver一个单独的类,然后注册您的广播接收器作为

<receiver android:name=".mReceiver1"> 
.... 
</receiver> 

如果你有你的BroadcastReceiver作为一个内部类注册为

<receiver android:name=".ActivityName$mReceiver1"> 
.... 
</receiver>