项目中要做媒体扫描,需要检测sd插拔事件。写了个demo分析下。

 mReceiver = new BroadcastReceiver() {            @Override            public void onReceive(Context context, Intent intent) {                onReceiveMediaBroadcast(intent);            }        };

//onReceive方法:private void onReceiveMediaBroadcast(Intent intent) {        String action = intent.getAction();        if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {        Log.i(Intent.ACTION_MEDIA_MOUNTED);        } else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) {        Log.i(Intent.ACTION_MEDIA_UNMOUNTED);        } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)) {        Log.i(Intent.ACTION_MEDIA_SCANNER_STARTED);        } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {        Log.i(Intent.ACTION_MEDIA_SCANNER_FINISHED);        } else if (action.equals(Intent.ACTION_MEDIA_BAD_REMOVAL)) {        Log.i(Intent.ACTION_MEDIA_BAD_REMOVAL);        }        else if (action.equals(Intent.ACTION_MEDIA_EJECT)) {        Log.i(Intent.ACTION_MEDIA_EJECT);        }        else if (action.equals(Intent.ACTION_MEDIA_REMOVED)) {        Log.i(Intent.ACTION_MEDIA_REMOVED);        }    }
//注册广播接收器:        IntentFilter intentFilter = new IntentFilter();        intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);        intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);        intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);        intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);        intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);        intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);        intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);        intentFilter.addDataScheme("file");        registerReceiver(mReceiver, intentFilter);

log信息:
插卡:系统打出来的状态,{removed}->{unmounted}->{checking}->{mounted}
我的程序打印信息,android.intent.action.MEDIA_UNMOUNTED->android.intent.action.MEDIA_MOUNTED
->android.intent.action.MEDIA_SCANNER_STARTED->android.intent.action.MEDIA_SCANNER_FINIDHED
拔卡:系统打出来的状态,{mounted}->{umounted}->{removed}
我的程序打印信息,android.intent.action.MEDIA_EJECT->android.intent.action.MEDIA_UNMOUNTED
->android.intent.action.MEDIA_REMOVED

更多相关文章

  1. Android(安卓)4.1 APIs
  2. Android实现Flip翻转动画效果
  3. Android:intent用法实例
  4. [Android]在代码里运行另一个程序的方法
  5. android判断当前应用是否启动、app运行状态、某个界面是否在前台
  6. Android中安全退出程序的六种方法
  7. Android应用程序组件Content Provider应用实例(4)
  8. Android从App跳转到微信小程序,无需微信SDK,防止友盟冲突
  9. Android通过Termux安装scrapy遇到的问题和解决方法

随机推荐

  1. Android沉浸式状态栏、导航栏
  2. android 中使文本(TextView 、button等可
  3. android 支付宝SDK集成
  4. Android 无法查看外部依赖jar的源码的问
  5. android 自定义SeekBar
  6. 百度地图 android 相关收集
  7. Android:GestureDetector手势识别类
  8. )Android之getSystemService
  9. Android软件汉化/精简/去广告/优化教程
  10. android获取图片的RGB颜色值