Code example for WifiManager.WifiLock

Methods: acquire, isHeld, release

0
            mPlayer.release();
            mPlayer = null;
        } 
 
        // we can also release the Wifi lock, if we're holding it 
        if (mWifiLock.isHeld()) mWifiLock.release();
    } 
 
    void giveUpAudioFocus() { 
        if (mAudioFocus == AudioFocus.Focused && mAudioFocusHelper != null
                                && mAudioFocusHelper.abandonFocus())
            mAudioFocus = AudioFocus.NoFocusNoDuck;
    } 
 
    /** 
     * Reconfigures MediaPlayer according to audio focus settings and starts/restarts it. This 
     * method starts/restarts the MediaPlayer respecting the current audio focus state. So if 
     * we have focus, it will play normally; if we don't have focus, it will either leave the 
     * MediaPlayer paused or set it to a low volume, depending on what is allowed by the 
     * current focus settings. This method assumes mPlayer != null, so if you are calling it, 
     * you have to do so from a context where you are sure this is the case. 
     */ 
    void configAndStartMediaPlayer() { 
        if (mAudioFocus == AudioFocus.NoFocusNoDuck) {
            // If we don't have audio focus and can't duck, we have to pause, even if mState 
            // is State.Playing. But we stay in the Playing state so that we know we have to resume 
            // playback once we get the focus back. 
            if (mPlayer.isPlaying()) mPlayer.pause();
            return; 
        } 
        else if (mAudioFocus == AudioFocus.NoFocusCanDuck)
            mPlayer.setVolume(DUCK_VOLUME, DUCK_VOLUME);  // we'll be relatively quiet
        else 
            mPlayer.setVolume(1.0f, 1.0f); // we can be loud
 
        if (!mPlayer.isPlaying()) mPlayer.start();
    } 
 
    void processAddRequest(Intent intent) {
    	Log.d(TAG, "WE ARE PROCESSING ADD REQUEST");
        // user wants to play a song directly by URL or path. The URL or path comes in the "data" 
        // part of the Intent. This Intent is sent by {@link MainActivity} after the user 
        // specifies the URL/path via an alert box. 
        if (mState == State.Retrieving) {
            // we'll play the requested URL right after we finish retrieving 
            mWhatToPlayAfterRetrieve = intent.getData();
            mStartPlayingAfterRetrieve = true;
        } 
        else if (mState == State.Playing || mState == State.Paused || mState == State.Stopped) {
            Log.i(TAG, "Playing from URL/path: " + intent.getData().toString());
            tryToGetAudioFocus(); 
            playNextSong(intent.getData().toString());
        } 
    } 
 
    /** 
     * Shortcut to making and displaying a toast. Seemed cleaner than repeating 
     * this code everywhere, at least for this sample. 
     */ 
    void say(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    } 
 
    void tryToGetAudioFocus() { 
        if (mAudioFocus != AudioFocus.Focused && mAudioFocusHelper != null
                        && mAudioFocusHelper.requestFocus())
            mAudioFocus = AudioFocus.Focused;
    } 
 
    /** 
     * Starts playing the next song. If manualUrl is null, the next song will be randomly selected 
     * from our Media Retriever (that is, it will be a random song in the user's device). If 
     * manualUrl is non-null, then it specifies the URL or path to the song that will be played 
     * next. 
     */ 
    void playNextSong(String manualUrl) {
        mState = State.Stopped;
        relaxResources(false); // release everything except MediaPlayer 
 
        try { 
            if (manualUrl != null) {
                // set the source of the media player to a manual URL or path 
                createMediaPlayerIfNeeded(); 
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mPlayer.setDataSource(manualUrl);
                mSongTitle = manualUrl;
                mIsStreaming = manualUrl.startsWith("http:") || manualUrl.startsWith("https:");
            } 
            else { 
                mIsStreaming = false; // playing a locally available song
 
                MusicRetriever.Item item = mRetriever.getRandomItem();
                if (item == null) {
                    say("No song to play :-("); 
                    return; 
                } 
 
                // set the source of the media player a a content URI 
                createMediaPlayerIfNeeded(); 
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                Log.e("MusicService", "Audio URI:" + item.getURI());
                mPlayer.setDataSource(getApplicationContext(), item.getURI());
                mSongTitle = item.getTitle();
            } 
 
 
            mState = State.Preparing;
            setUpAsForeground(mSongTitle + " (loading)");
 
            // starts preparing the media player in the background. When it's done, it will call 
            // our OnPreparedListener (that is, the onPrepared() method on this class, since we set 
            // the listener to 'this'). 
            // 
            // Until the media player is prepared, we *cannot* call start() on it! 
            mPlayer.prepareAsync();
 
            // If we are streaming from the internet, we want to hold a Wifi lock, which prevents 
            // the Wifi radio from going to sleep while the song is playing. If, on the other hand, 
            // we are *not* streaming, we want to release the lock if we were holding it before. 
            if (mIsStreaming) mWifiLock.acquire();
            else if (mWifiLock.isHeld()) mWifiLock.release();
        } 
        catch (IOException ex) {
            Log.e("MusicService", "IOException playing next song: " + ex.getMessage());
            ex.printStackTrace();
        }