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

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

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

.......net

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();blog

registerReceiver(sdCardStateReceiver, sdcardActionFilter);事件

......get

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)
) {
...... 
}
}
}io

相关文章
相关标签/搜索