SD卡监听---BroadcastReceiver

本文介绍如何在Android应用中监听并响应SD卡的状态变化,包括SD卡就绪、拔出和卸载,通过注册广播接收器实现SD卡状态的实时监控。
package com.demo.sdcard;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class SDReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		//判断收到的广播类型
		//获取广播中的action
		String action = intent.getAction();
		
		if(Intent.ACTION_MEDIA_MOUNTED.equals(action)){
			Toast.makeText(context, "sd卡就绪", 0).show();
		}else if(Intent.ACTION_MEDIA_REMOVED.equals(action)){
			Toast.makeText(context, "sd卡拔出", 0).show();
		}else if(Intent.ACTION_MEDIA_UNMOUNTED.equals(action)){
			Toast.makeText(context, "sd卡卸载", 0).show();
		}
	}

}

xml注册广播和添加sd卡状态
<pre name="code" class="java"><receiver 
            android:name="com.demo.sdcard.SDReceiver">
            <intent-filter >
                <!-- 添加SD卡的状态    就绪,拔出,卸载 -->
                <action android:name="android.intent.action.MEDIA_MOUNTED"/>
                <action android:name="android.intent.action.MEDIA_REMOVED"/>
                <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
            </intent-filter>
        </receiver>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值