<Android> 监听MEDIA_MOUNTED

本文介绍了一个用于监听Android设备上SD卡状态变化的BroadcastReceiver实现。该实现通过注册Intent过滤器来接收不同类型的SD卡事件,如插入、移除、挂载、卸载等,并展示了如何根据不同事件采取相应的操作。

private final BroadcastReceiver myMediaReceiver = new BroadcastReceiver() {
    @Override  
    public void onReceive(final Context context, final Intent intent) { 
        String action = intent.getAction();  
        Log.i(MYTAG, String.format("broadcast %s received", action));
        if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
            // ...
        }

    }
};

private void registerMedidReceiver() {
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);// sd卡被插入,且已经挂载    
    intentFilter.setPriority(1000);// 设置最高优先级    
    intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);// sd卡存在,但还没有挂载    
    intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);// sd卡被移除    
    intentFilter.addAction(Intent.ACTION_MEDIA_SHARED);// sd卡作为 USB大容量存储被共享,挂载被解除    
    intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);// sd卡已经从sd卡插槽拔出,但是挂载点还没解除    
    intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);// 开始扫描    
    intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);// 扫描完成    
    intentFilter.addDataScheme("file");    
    registerReceiver(myMediaReceiver, intentFilter);// 注册监听函数    
}
@startuml USBOfflineUpdateDetailedProcess skinparam { BackgroundColor #F8F8F8 ArrowColor #333333 ActorBorderColor #5D8AA8 ActorBackgroundColor #E6F2FF ComponentBorderColor #FF8000 ComponentBackgroundColor #FFF2E6 LifeLineBorderColor #6B8E23 LifeLineBackgroundColor #F5F5F5 SequenceMessageAlignment reverse SequenceMessageColor #006400 SequenceArrowColor #8B4513 BoxBorderColor #4682B4 BoxBackgroundColor #F0F8FF GroupBorderColor #800080 GroupBackgroundColor #F5F0FA TitleFontSize 16 TitleFontColor #2F4F4F DefaultFontName Arial DefaultFontSize 12 DefaultTextAlignment center } title **<color:#2F4F4F>USB离线更新系统详细流程**</color> actor **用户** as user #5D8AA8 participant "<color:#006400><b>UI界面</b></color>" as ui #E6F2FF participant "<color:#8B0000><b>USBOfflineUpdater</b></color>" as updater #FFE4E1 participant "<color:#006400><b>UpdateTask</b></color>" as task #E6FFE6 participant "<color:#4B0082><b>FileUtils</b></color>" as files #E6E6FA participant "<color:#8B4513><b>StorageUtils</b></color>" as storage #FFEBCD participant "<color:#2E8B57><b>MapDataController</b></color>" as map #E0FFE0 box "<color:#4682B4><b>Android系统</b></color>" #F0F8FF participant "<color:#800080><b>USB监听器</b></color>" as usb #F5F0FA participant "<color:#D2691E><b>电量服务</b></color>" as power #FFF8DC end box == <color:#2F4F4F>初始化阶段</color> == user -> ui : **启动应用** ui -> updater : **getInstance(context)** activate updater #FFA07A updater -> updater : **初始化目录结构** updater -> usb : **注册监听器** updater -> power : **获取电量状态** updater -> ui : **返回实例** deactivate updater == <color:#2F4F4F>检测阶段</color> == loop **<color:#8B0000>等待USB插入</color>** usb -> updater : **onReceive(ACTION_MEDIA_MOUNTED)** activate updater #FFA07A updater -> files : **扫描升级包** alt <color:#FF0000>未找到升级包</color> updater -> ui : **显示ERROR_NO_UPDATE_PACKAGE** else <color:#008000>找到升级包</color> updater -> storage : **检查存储空间** updater -> power : **检查电量** alt <color:#FF0000>环境检测失败</color> updater -> ui : **显示对应错误码** else <color:#008000>检测通过</color> updater -> ui : **显示"准备更新"按钮** end end deactivate updater end == <color:#2F4F4F>更新执行阶段</color> == user -> ui : **点击"开始更新"** ui -> updater : **startUpdate(listener)** activate updater #FFA07A updater -> task : **创建UpdateTask** activate task #98FB98 group **<color:#800080>备份阶段 [PHASE_BACKUP]</color>** #F5F0FA task -> files : **创建备份目录** task -> files : **计算备份大小** loop **<color:#4B0082>文件复制进度</color>** task -> files : **复制系统数据** files -> task : **进度回调(currentCopiedBytes)** task -> ui : **更新进度(<color:#0000FF>10%</color>)** end end group **<color:#800080>升级包处理 [循环]</color>** #F5F0FA loop **每个升级包** group **<color:#006400>复制阶段 [PHASE_COPYING]</color>** #E6FFE6 task -> files : **拷贝升级包** files -> task : **进度回调** task -> ui : **更新进度(<color:#0000FF>29%</color>)** end group **<color:#8B4513>校验阶段 [PHASE_CHECKING]</color>** #FFEBCD task -> updater : **解密验签** updater -> task : **进度回调** task -> ui : **更新进度(<color:#0000FF>31%</color>)** end group **<color:#2E8B57>解压阶段 [PHASE_EXTRACTING]</color>** #E0FFE0 task -> files : **解压升级包** files -> task : **进度回调** task -> ui : **更新进度(<color:#0000FF>29%</color>)** end end end group **<color:#800080>完成阶段 [PHASE_CLEANUP]</color>** #F5F0FA task -> files : **清理临时文件** task -> map : **校验安装状态** map -> task : **返回校验结果** task -> ui : **显示"<color:#008000>更新成功</color>"** end == <color:#2F4F4F>用户操作处理</color> == alt **<color:#FF0000>用户点击暂停</color>** user -> ui : **点击"暂停"** ui -> updater : **pauseUpdate()** updater -> task : **设置isPaused=true** task -> ui : **显示"已暂停"** end alt **<color:#008000>用户点击继续</color>** user -> ui : **点击"继续"** ui -> updater : **resumeUpdate()** updater -> task : **设置isPaused=false** task -> ui : **恢复进度显示** end alt **<color:#8B0000>用户点击取消</color>** user -> ui : **点击"取消"** ui -> updater : **cancelUpdate()** updater -> task : **设置isCancelled=true** task -> updater : **触发回滚流程** end == <color:#2F4F4F>异常处理</color> == alt **<color:#FF0000>拷贝失败</color>** task -> updater : **返回ERROR_COPY_FAILED** updater -> ui : **显示错误弹窗** end alt **<color:#FF0000>验签失败</color>** task -> updater : **返回ERROR_DECRYPT_OR_SIGN_FAILED** updater -> ui : **显示"文件校验失败"** end group **<color:#800080>回滚流程 [PHASE_ROLLBACK]</color>** #F5F0FA updater -> files : **恢复备份数据** loop **<color:#4B0082>恢复进度</color>** files -> updater : **进度回调** updater -> ui : **显示"正在回滚(<color:#0000FF>99%</color>)"** end updater -> files : **删除备份目录** updater -> ui : **显示"已恢复原始状态"** end == <color:#2F4F4F>资源释放</color> == user -> ui : **退出应用** ui -> updater : **release()** updater -> usb : **注销监听器** updater -> files : **清理临时文件** @enduml 报出语法错误,请你纠正 28行位于图表"USBOfflineUpdateDetailedProcess"中的错误 ERROR 27 Syntax Error? Some diagram description contains errors
08-29
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.isa.navi" android:sharedUserId="android.uid.system" android:versionCode="1" android:versionName="V0.9.0_20250621"> <!-- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />--> <!-- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />--> <!-- <uses-permission android:name="android.permission.INTERNET" />--> <!-- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />--> <!-- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />--> <!-- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />--> <!-- <uses-permission android:name="android.permission.USB_PERMISSION"/>--> <!-- <uses-permission android:name="android.permission.WAKE_LOCK" />--> <!-- <uses-permission android:name="android.permission.READ_PHONE_STATE" />--> <!-- <uses-permission android:name="android.permission.ACCESS_MAP_ISA"/>--> <application android:name="com.isa.navi.ui.ISANaviApplication" android:allowBackup="false" android:label="ISA-NaviService" android:supportsRtl="true" android:networkSecurityConfig="@xml/network_security_config" android:taskAffinity="" android:persistent="true" > <!-- android:debuggable="true" --> <service android:name="com.isa.navi.service.ISANaviService" android:directBootAware="true" android:exported="false"> <!-- android:permission="com.isa.permission.ACCESS"--> <!-- >--> <intent-filter> <action android:name="com.isa.intent.action.ISA_NAVI" /> <action android:name="com.isa.intent.action.ISA_NAVI_DEBUG" /> <action android:name="com.isa.intent.action.ISA_CARPLAY" /> </intent-filter> </service> <receiver android:name="com.isa.navi.BootCompleteReceive" android:directBootAware="true" android:exported="false" > <!-- 开机广播 --> <!-- <intent-filter>--> <!-- <action android:name="android.intent.action.BOOT_COMPLETED" />--> <!-- </intent-filter>--> <intent-filter android:priority="2147483647"> <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" /> </intent-filter> </receiver> <!-- <activity android:name=".ui.MainActivity">--> <!-- <intent-filter>--> <!-- <action android:name="android.intent.action.MAIN" />--> <!-- <category android:name="android.intent.category.LAUNCHER" />--> <!-- </intent-filter>--> <!-- </activity>--> <receiver android:name=".receiver.USBReceiver" android:exported="false"> <intent-filter android:priority="1000"> <action android:name="android.intent.action.BOOT_COMPLETED"/> <action android:name="android.intent.action.MEDIA_MOUNTED"/> <action android:name="android.intent.action.MEDIA_UNMOUNTED" /> <action android:name="android.intent.action.MEDIA_REMOVED"/> <data android:scheme="file"></data> </intent-filter> </receiver> </application> </manifest> 这个xml是什么
07-31
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值