监听插拔SD卡

本文介绍了如何使用Intent.ACTION_MEDIA_EJECT和ACTION_MEDIA_MOUNTED来监听SD卡的拔出和装入操作,并强调了在监听过程中,如果SD卡被拔出而MediaPlayer正在使用其资源,会导致MEDIA_ERROR_SERVER_DIED错误。当发生该错误时,应用需要释放MediaPlayer对象并创建新的实例。同时提供了解决错误的监听器代码。
摘要由CSDN通过智能技术生成

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值