接收广播的mReceiver:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_MOUNTED)
|| action.equals(Intent.ACTION_MEDIA_REMOVED)
|| action.equals(Intent.ACTION_MEDIA_BAD_REMOVAL)
|| action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) {
// do something
}
}
};
广播的注册:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
//下面这行代码至关重要,否则注册的广播接收不到sdcard的插拔事件
intentFilter.addDataScheme("file");
mActivity.registerReceiver(mReceiver, intentFilter);
取消广播的注册:
mActivity.unregisterReceiver(mReceiver);