android监听SD卡状态
16lz
2022-05-27
步骤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"/>
不加监听不到。。
更多相关文章
- android 按钮按下效果2
- Android的Button监听
- Android中点击事件之KeyListener实现步骤
- android实现事件监听的2种常用方式
- Android(安卓)短信验证码自动填写
- Android(安卓)安装步骤
- android 监听EditText 复制粘贴等操作
- Android(安卓)百度地图 对鼠标点击、移动、抬起三个事件的监听
- Android(安卓)底部导航BottomNavigationView(非官方)