import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* 1. 买收音机
*/
public class SDReceiver extends BroadcastReceiver {
/**
* 系统调用
* 接受到广播后调用
*/
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ("android.intent.action.MEDIA_UNMOUNTED".equals(action)) {
System.out.println("SD卡被拔出");
}else if("android.intent.action.MEDIA_MOUNTED".equals(action)){
System.out.println("SD卡被插入了");
}
}
}
清单文件: <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<!-- 2. 装电池 -->
<receiver android:name="com.example.sdmonitor.SDReceiver" >
<!-- 3. 调频道-->
<intent-filter >
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<!-- 重要 -->
<data android:scheme="file"/>
</intent-filter>
</receiver>