自定义广播

时间:2014-04-30 02:29:49   收藏:0   阅读:514
mamicode.com,码迷
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);
mamicode.com,码迷

 

mamicode.com,码迷
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();
              }
            
        }
        
    }
mamicode.com,码迷
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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);
        }
         
         
    }

  

 

 

 

自定义广播,码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!