mSDStateBrocast = new SDStateBrocast(); IntentFilter intentFilter2 = new IntentFilter(); intentFilter2.addAction(Intent.ACTION_MEDIA_MOUNTED); intentFilter2.addAction(Intent.ACTION_MEDIA_UNMOUNTED); intentFilter2.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED); intentFilter2.addAction(Intent.ACTION_MEDIA_EJECT); intentFilter2.addDataScheme("file"); registerReceiver(mSDStateBrocast, intentFilter2);
class SDStateBrocast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) { // Log.i(TAG, "===================>Intent.ACTION_MEDIA_MOUNTED"); mIsSdExist = true; }else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) { // Log.i(TAG, "===================>Intent.ACTION_MEDIA_UNMOUNTED"); mIsSdExist = false; }else if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action)) { // Log.i(TAG, "===================>Intent.ACTION_MEDIA_SCANNER_FINISHED mIsSdExist = " + mIsSdExist); if (mIsSdExist) { m_MusicFileList = getMusicFileList(); mServiceManager.refreshMusicList(m_MusicFileList); if (m_MusicFileList.size() > 0) { mIsHaveData = true; } mListViewAdapter.refreshAdapter(m_MusicFileList); } }else if (Intent.ACTION_MEDIA_EJECT.equals(action)) { // Log.i(TAG, "===================>Intent.ACTION_MEDIA_EJECT"); // mServiceManager.reset(); m_MusicFileList.clear(); mListViewAdapter.refreshAdapter(m_MusicFileList); mIsHaveData = false; mUIManager.emptyPlayInfo(); } } }
public void sendPlayStateBrocast() { if (mContext != null) { Intent intent = new Intent(BROCAST_NAME); intent.putExtra(MusicPlayState.PLAY_STATE_NAME, mPlayState); intent.putExtra(MusicPlayState.PLAY_MUSIC_INDEX, mCurPlayIndex); if (mPlayState != MusicPlayState.MPS_NOFILE) { Bundle bundle = new Bundle(); MusicData data = mMusicFileList.get(mCurPlayIndex); bundle.putParcelable(MusicData.KEY_MUSIC_DATA, data); intent.putExtra(MusicData.KEY_MUSIC_DATA, bundle); } mContext.sendBroadcast(intent); } }