admin管理员组

文章数量:1567138

1、在AndroidManifest.xml 加入读取外部存储器权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


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");
}
}

};

本文标签: android