步骤1、创建一个广播类

public class SdReceiver extends BroadcastReceiver {    private String TAG = "TfReceiver";    @Override    public void onReceive(Context context, Intent intent) {        String action = intent.getAction();        switch (action) {            //卡已经成功挂载            case "android.intent.action.MEDIA_MOUNTED":                String path = StoragePathUtil.getInstance().getPath(true);                Log.d(TAG, "onReceive: MEDIA_MOUNTED--" + path);                break;            //卡未挂载            case "android.intent.action.MEDIA_EJECT":            case "android.intent.action.MEDIA_REMOVED":            case "android.intent.action.ACTION_MEDIA_UNMOUNTED":            case "android.intent.action.ACTION_MEDIA_BAD_REMOVAL":                Log.d(TAG, "onReceive: 未挂载");                break;            //开始扫描            case "android.intent.action.MEDIA_SCANNER_STARTED":                Log.d(TAG, "onReceive: 开始扫描...");                break;            //扫描完成            case "android.intent.action.MEDIA_SCANNER_FINISHED":                Log.d(TAG, "onReceive: 扫描完成...");                break;            case "android.intent.action.MEDIA_SHARED":                Log.d(TAG, "onReceive: USB大容量存储被共享...");                break;        }    }}

步骤2、在AndroidManifest中注册静态监听(动态的也可以)

        <receiver android:name=".receiver.TfReceiver">            <intent-filter android:priority="1000">                <action android:name="android.intent.action.MEDIA_MOUNTED" />                <action android:name="android.intent.action.MEDIA_EJECT" />                  <action android:name="android.intent.action.MEDIA_REMOVED" />                <action android:name="android.intent.action.ACTION_MEDIA_UNMOUNTED" />                <action android:name="android.intent.action.ACTION_MEDIA_BAD_REMOVAL" />                <action android:name="android.intent.action.MEDIA_SCANNER_STARTED" />                <action android:name="android.intent.action.MEDIA_SCANNER_FINISHED" />                <action android:name="android.intent.action.MEDIA_SHARED" />                <data android:scheme="file"/>            intent-filter>        receiver>

插入是监听android.intent.action.MEDIA_MOUNTED
拔出是监听android.intent.action.MEDIA_EJECT

主要记录下要注册

<data android:scheme="file"/>

不加监听不到。。

更多相关文章

  1. android 按钮按下效果2
  2. Android的Button监听
  3. Android中点击事件之KeyListener实现步骤
  4. android实现事件监听的2种常用方式
  5. Android(安卓)短信验证码自动填写
  6. Android(安卓)安装步骤
  7. android 监听EditText 复制粘贴等操作
  8. Android(安卓)百度地图 对鼠标点击、移动、抬起三个事件的监听
  9. Android(安卓)底部导航BottomNavigationView(非官方)

随机推荐

  1. 使用远程调试Android设备
  2. 转:listview android:cacheColorHint,andr
  3. Permissions
  4. Android(安卓)中不弹出软键盘的方法
  5. android 静音方法
  6. 蓝牙HID无线触摸屏
  7. Android(安卓)SDK相关介绍
  8. Android统计图表MPAndroidChart
  9. Android对话框里面的输入值获取不到,空指
  10. android九宫格实现