SD卡状态变动receiver接收不到的问题

用BroadcastReceiver接收SD卡状态变化的事件时,总是无法收到事件通知,经查是少了以下红色语句。

原因涉及Android中IntentFilter匹配原则问题。这篇文章讲得比较清楚http://blog.csdn.net/silenceburn/article/details/6083375 , 这里就不再赘述。

......

IntentFilter sdcardActionFilter = new IntentFilter();
sdcardActionFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
sdcardActionFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
sdcardActionFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
sdcardActionFilter.addDataScheme("file");
SDcardListenerReceiver sdCardStateReceiver = new SDcardListenerReceiver();

registerReceiver(sdCardStateReceiver, sdcardActionFilter);

......

static final class SDcardListenerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_REMOVED)
  || action.equals(Intent.ACTION_MEDIA_UNMOUNTED)
  || action.equals(Intent.ACTION_MEDIA_BAD_REMOVAL)
) {
...... 
}
}
}

 

posted @ 2013-09-05 14:02  shtzsp  阅读(198)  评论(0编辑  收藏  举报