2011-12-15 108 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>