public void registerExternalStorageListener() {
if (mUnmountReceiver == null) {
mUnmountReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (mReceiverUnregistered) {
return;
}
String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
LogUtil.d(TAG, "MEDIA_EJECT");
// For multi-SD card, we should decide whether the
// internal SD card is ejected
// String ejectCardPath = intent.getData().getPath();
mEjectingCardPath = intent.getData().getPath();
LogUtil.d(TAG, "ejected card path=" + mEjectingCardPath);
LogUtil.d(TAG, "main card path="
+ Environment.getExternalStorageDirectory().getPath());
boolean isPlayingSongInSdCard = false;
String songPath = mPlayQueue.getCurSongPath();
LogUtil.d(TAG, "ejected card songPath=" + songPath);
if(songPath != null && songPath.contains(mEjectingCardPath)) {
isPlayingSongInSdCard = true;
}
if (mEjectingCardPath.equals(Environment.getExternalStorageDirectory().getPath()) || isPlayingSongInSdCard) {
// internal card is being ejected
LogUtil.d(TAG, "MEDIA_EJECT: internal card unmounting...");
saveQueue(true);
mQueueIsSaveable = false;
mCardId = -1;
LogUtil.d(TAG, "card eject: cardid=" + mCardId);
}
mEjectingCardPath = null;
} else if (action.equals(Intent.AC
监听插拔SD卡
最新推荐文章于 2023-12-05 18:28:04 发布
本文介绍了如何使用Intent.ACTION_MEDIA_EJECT和ACTION_MEDIA_MOUNTED来监听SD卡的拔出和装入操作,并强调了在监听过程中,如果SD卡被拔出而MediaPlayer正在使用其资源,会导致MEDIA_ERROR_SERVER_DIED错误。当发生该错误时,应用需要释放MediaPlayer对象并创建新的实例。同时提供了解决错误的监听器代码。
摘要由CSDN通过智能技术生成