項目中要做媒體掃描,需要檢測sd插拔事件。寫了個demo分析下。
[java]
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
onReceiveMediaBroadcast(intent);
}
};
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
onReceiveMediaBroadcast(intent);
}
};
[java]
//onReceive方法:
private void onReceiveMediaBroadcast(Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
Log.i(Intent.ACTION_MEDIA_MOUNTED);
} else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) {
Log.i(Intent.ACTION_MEDIA_UNMOUNTED);
} else if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)) {
Log.i(Intent.ACTION_MEDIA_SCANNER_STARTED);
} else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
Log.i(Intent.ACTION_MEDIA_SCANNER_FINISHED);
} else if (action.equals(Intent.ACTION_MEDIA_BAD_REMOVAL)) {
Log.i(Intent.ACTION_MEDIA_BAD_REMOVAL);
}
else if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
Log.i(Intent.ACTION_MEDIA_EJECT);
}
else if (action.equals(Intent.ACTION_MEDIA_REMOVED)) {
Log.i(Intent.ACTION_MEDIA_REMOVED);
}
}
//onReceive方法:
private void onReceiveMediaBroadcast(Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
Log.i(Intent.ACTION_MEDIA_MOUNTED);
} else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) {
Log.i(Intent.ACTION_MEDIA_UNMOUNTED);
} else if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)) {
Log.i(Intent.ACTION_MEDIA_SCANNER_STARTED);
} else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
Log.i(Intent.ACTION_MEDIA_SCANNER_FINISHED);
} else if (action.equals(Intent.ACTION_MEDIA_BAD_REMOVAL)) {
Log.i(Intent.ACTION_MEDIA_BAD_REMOVAL);
}
else if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
Log.i(Intent.ACTION_MEDIA_EJECT);
}
else if (action.equals(Intent.ACTION_MEDIA_REMOVED)) {
Log.i(Intent.ACTION_MEDIA_REMOVED);
}
}
[java]
//註冊廣播接收器:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
intentFilter.addDataScheme("file");
registerReceiver(mReceiver, intentFilter);
//註冊廣播接收器:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
intentFilter.addDataScheme("file");
registerReceiver(mReceiver, intentFilter);
log資訊:
插卡:系統打出來的狀態,{removed}->{unmounted}->{checking}->{mounted}
我的程式列印資訊,android.intent.action.MEDIA_UNMOUNTED->android.intent.action.MEDIA_MOUNTED
->android.intent.action.MEDIA_SCANNER_STARTED->android.intent.action.MEDIA_SCANNER_FINIDHED
拔卡:系統打出來的狀態,{mounted}->{umounted}->{removed}
我的程式列印資訊,android.intent.action.MEDIA_EJECT->android.intent.action.MEDIA_UNMOUNTED
->android.intent.action.MEDIA_REMOVED
摘自 痛而不言,笑而不語