Android(安卓)监听外部U盘插入
1、在AndroidManifest.xml 加入读取外部存储器权限
2、注册动态监听U盘插入的广播
IntentFilter filter = null;
filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED); //接受外媒挂载过滤器
filter.addAction(Intent.ACTION_MEDIA_REMOVED); //接受外媒挂载过滤器
filter.addDataScheme("file");
registerReceiver(mSdcardReceiver, filter,"android.permission.READ_EXTERNAL_STORAGE",null);
BroadcastReceiver mSdcardReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)){
Toast.makeText(context, "path:"+intent.getData().getPath(), Toast.LENGTH_SHORT).show();
}else if(intent.getAction().equals(Intent.ACTION_MEDIA_REMOVED)){
Log.i("123", "remove ACTION_MEDIA_REMOVED");
}
}
};
更多相关文章
- Cocos2d-x + Android(安卓)+ Eclipse + Windows 8
- Android如何接收locale改变的消息 || locale改变后,发送什么消息
- 【android】Android(安卓)7.0适配步骤
- Android判断定位功能是否可用
- Android(安卓)检测是否有ROOT权限
- Linux Mint下Kindle Fire调试android程序
- Android权限控制EasyPermissions
- android 打开GPS的几种方式
- Android调试工具之Logcat