Java 安卓USB连接状态;
我想在SQL SERVER和SQLite之间进行复制。它使用WIFI网络连接进行复制 我的问题是:网络优先USB(连接到PC)、GPRS、WIFI。这是优先顺序。 Wifi和GPRS都可以。USB是一个问题。如何识别连接到PC的Android设备Java 安卓USB连接状态;,java,android,Java,Android,我想在SQL SERVER和SQLite之间进行复制。它使用WIFI网络连接进行复制 我的问题是:网络优先USB(连接到PC)、GPRS、WIFI。这是优先顺序。 Wifi和GPRS都可以。USB是一个问题。如何识别连接到PC的Android设备 public class DownlaodTableActivity extends Activity{ private int networkType; private int signalStrengthInt
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>
请告诉我为什么不使用USBSttaus=true;
我在PC上连接了Android,但仍然显示usb设备连接状态为false
请帮我解决这个问题
提前感谢 我认为您在清单文件中声明您的接收者是错误的
应该是,
<receiver android:name=".IntentReceiver">
<intent-filter>
<action android:name="android.intent.action.UMS_CONNECTED" />
</intent-filter>
</receiver>
我会坚持要求您创建一个内部类或为BroadcastReceiver
创建一个单独的类。似乎您已经在清单
文件中声明了您的广播接收器
。因此,如果您有一个单独的类用于BroadcastReceiver
,那么将您的BroadcastReceiver注册为
<receiver android:name=".mReceiver1">
....
</receiver>
更重要的是你将如何通过USB将数据传输到PCI真的不知道我将如何处理。在我的本地PC上,它运行着服务。然后它会自动下载数据。这个链接对我很有帮助。谢谢
<receiver android:name=".ActivityName$mReceiver1">
....
</receiver>