Android USB附加状态;

时间:2011-12-15 04:25:35

标签: java android

我想在SQL SERVER和&之间进行复制。 SQLite.ITs使用WIFI网络连接。

我的问题是:网络优先级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设备sttached状态为false。

请帮助解决这个问题。

提前致谢;

2 个答案:

答案 0 :(得分:2)

我认为你在Manifest文件中声明你的收件人是错误的。

应该是,

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

请参阅此处,了解有关在 This Page

的上一篇文章中启用USB连接服务的完整资源

答案 1 :(得分:2)

我会坚持要你创建一个内部类或为BroadcastReceiver创建一个单独的类。您似乎已在BroadcastReceiver文件中声明了Manifest。因此,如果您有BroadcastReceiver的单独类,则将BroadcastReceiver注册为

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

如果您将BroadcastReceiver作为内部类注册为

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