项目中要得到USB的连接状态,在3.1以前的版本中,有一个ACTION_UMS_CONNECTED广播,表示手机已进入大容量模式,但是没有消息说USB有没有连上 。3.1以上的版本包含了一个android.hardware.usb ,也没有相应的API。其实我们可以从batterymanager入手:
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(BtStatusReceiver, mIntentFilter);
public BroadcastReceiver BtStatusReceiver = new BroadcastReceiver() // receive broadcast that BT Adapter status change
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED))
{
Log.d("Battery", "" + intent.getIntExtra("plugged", 0));
Toast text = Toast.makeText(context, "ACTION_USB_DEVICE_ATTACHED"+intent.getIntExtra("plugged", 0), Toast.LENGTH_LONG);
text.show();
dataview.setText(""+intent.getIntExtra("plugged", 0));
}
}
};
intent.getIntExtra("plugged", 0)得到的值分别为:
0:断开
1:连上USB
2:连上了充电器
----------------------------------------------------------------------------------------------------------------------------------------------
private final BroadcastReceiver broadcastRec = newBroadcastReceiver(){
public void onReceive(Context context, Intent intent){
if(intent.getAction().equals("android.intent.action.MEDIA_MOUNTED"))
{
File file=newFile("/sdcard/card/");
File file2=newFile("/usb/");
if(file.exists() && !isSdcard) { //判断SD卡 插入
isSdcard=true;
Log.d("explorer","SDcard is useful!");
Toast.makeText(context, R.string.SDCardin, Toast.LENGTH_LONG).show();
btn_sdcard.setEnabled(true);
String strPath="/sdcard/card/";
path_list.AddPathToList(strPath);
updateFileList();
}
elseif(file2.exists() && !isUsb) { //判断usb 接入
//
isUsb=true;
Log.d("explorer","USB is useful!");
Toast.makeText(context, R.string.USBin, Toast.LENGTH_LONG).show();
btn_usb.setEnabled(true);
String strPath="/usb/";
path_list.AddPathToList(strPath);//显示当前路径为usb
updateFileList();//更新文件显示列表
}
}elseif(intent.getAction().equals("android.intent.action.MEDIA_REMOVED")
||intent.getAction().equals("android.intent.action.MEDIA_UNMOUNTED")
||intent.getAction().equals("android.intent.action.MEDIA_BAD_REMOVAL")){
File file=newFile("/sdcard/card/");
File file2=newFile("/usb/");
if(!file.exists() && isSdcard) {
Log.d("explorer","SDcard is not useful!");
Toast.makeText(context, R.string.SDCardout, Toast.LENGTH_LONG).show();
isSdcard=false;
btn_sdcard.setEnabled(false);
if(path_list.GetCurPath().startsWith("/sdcard/")){
select_list.ClearList();
path_list.AddPathToList("/");
}
}
elseif(!file2.exists() && isUsb){
isUsb=false;
Log.d("explorer","USB is not useful!");
Toast.makeText(context, R.string.USBout, Toast.LENGTH_LONG).show();
btn_usb.setEnabled(false);
if(path_list.GetCurPath().startsWith("/usb/")){
select_list.ClearList();
path_list.AddPathToList("/");
}
}
updateFileList();
}
}
};
private void initList(){
File file=newFile("/sdcard/card/");
if(file.exists()) {
Log.d("explorer","SDcard is useful!");
isSdcard=true;
btn_sdcard.setEnabled(true);
}else{
// 当前不可用
isSdcard=false;
Log.d("explorer","SDcard is no use!");
btn_sdcard.setEnabled(false);
}
String strPath="/";
File file2=newFile("/usb/");
if(file2.exists()) {
isUsb=true;
btn_usb.setEnabled(true);
if(isSdcard) {
strPath="/sdcard/card/";
}else{
strPath="/usb/";
}
}else{
isUsb=false;
btn_usb.setEnabled(false);
if(isSdcard) {
strPath="/sdcard/card/";
}else{
strPath="/";
}
}
path_list.AddPathToList(strPath);
updateFileList();
}
path=path.substring(11)+"/";//path=file:///mnt/sdcard/external_sdcard