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>