android外接存储设备状态判断-USB和SD卡连接状态
来源:互联网 发布:中文词频统计软件 编辑:程序博客网 时间:2024/06/07 10:06
项目中要得到USB的连接状态,在3.1以前的版本中,有一个ACTION_UMS_CONNECTED广播,表示手机已进入大容量模式,但是没有消息说USB有没有连上
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(); } } };
以上是广播接收外接存储插拔状态的。还有就是程序启动时判断usb和sdcard是否存在的:
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(); }
最近总有人问能否同时识别多个存储设备,我提供个思路,没有试验过:
在android广播插入存储设备时,它的intent中有data,用方法getDataString()获取其中数据你会发现那里面是这个设备的路径,你根据自己的需要截取路径,并根据这个来识别不同的存储设备。
String path=intent.getDataString();
path=path.substring(11)+"/";//path=file:///mnt/sdcard/external_sdcard
- android外接存储设备状态判断-USB和SD卡连接状态
- 论android外接存储设备状态判断
- android外接存储设备状态判断
- android usb大容量存储时sd卡状态监听
- android usb大容量存储时sd卡状态监听
- Android存储设备(U盘,SD卡)状态监测
- Android存储设备(U盘,SD卡)状态监测(《Android 2.3 SD卡挂载流程浅析1234567)
- android设备判断网络连接状态
- android 获取内部存储方式和sd卡状态
- Android中判断SD卡状态
- Android判断设备网络连接状态,并判断连接方式
- Android判断设备网络连接状态,并判断连接方式
- Android判断设备网络连接状态,并判断连接方式
- Android判断设备网络连接状态,并判断连接方式
- Android判断设备网络连接状态,并判断连接方式
- Android判断设备网络连接状态,并判断连接方式
- Android关于usb大容量存储时sd卡状态监听
- Android监听外部存储设备的状态(SD卡、U盘等等)
- ACE的安装
- 在Myeclipse buildpath 加server lib (server runtime)
- linux下的dc计算器
- WINDOW TYPE IN SMARTFORMS
- Entity Framework快速入门笔记第2篇—增删改查
- android外接存储设备状态判断-USB和SD卡连接状态
- java 链表的实现
- ARM开发板开发问题汇总
- Learning Languages (并查集)
- ZOJ 1003 Crashing Balloon 搜索
- 学科前沿技术(未来操作系统发展趋势)
- 安卓学习之
- 第五次C程序设计上机报告
- strcpy,sprintf,memcpy的区别, Sizeof与Strlen的区别