[英]Android USB attached Status;
I want to do the replication between SQL SERVER & SQLite.ITs wroking with WIFI netconnection. 我想在SQL SERVER和SQLite.ITs之间进行复制,使用WIFI网络连接。
My Problem is: Network priority USB(Connect to PC),GPRS,WIFI. 我的问题是:网络优先级USB(连接到PC),GPRS,WIFI。 This is the prioprity order.
这是先决条件。 Wifi & GPRS are ok.
Wifi和GPRS都可以。 USB is a problem.
USB是个问题。 How can i identiify Android device attached to PC.
如何识别连接到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) { ...........}
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 我也尝试过这种方式...... http://pastie.org/3019828
Please Tell me Why it didn't take USBSttaus =true; 请告诉我为什么没有USBSttaus = true; I connected Android in PC.But still it show usb device sttached status is false.
我在PC上连接了Android。但是它仍然显示usb设备sttached状态为false。
Please help out from this question. 请帮助解决这个问题。
Thanks in advance; 提前致谢;
You have mistaken in declaring your reciever in Manifest file I think. 我认为你在Manifest文件中声明你的接收器是错误的。
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 有关在本页的最后一篇文章中启动USB连接服务的完整信息,请参阅此处
I would insist you to create an inner class or create a seperate class for BroadcastReceiver
. 我坚持要你创建一个内部类或为
BroadcastReceiver
创建一个单独的类。 It seems you have declared your BroadcastReceiver
in the Manifest
file. 您似乎已在
Manifest
文件中声明了您的BroadcastReceiver
。 So, if you have a seperate class for BroadcastReceiver
then register your BroadcastReceiver as 因此,如果您有一个单独的
BroadcastReceiver
类,则将您的BroadcastReceiver注册为
<receiver android:name=".mReceiver1">
....
</receiver>
And if you have your BroadcastReceiver
as an inner class register as 如果你有你的
BroadcastReceiver
作为内部类注册
<receiver android:name=".ActivityName$mReceiver1">
....
</receiver>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.