Code Analysis

 

Code analysis for a sample application included in the LG FMRadio SDK will show how to develop an FM radio application.

The sample application consists of the following files:

 

File

Description

src/main/java/fmradio/lge/com/lgfmradioapp/MainActivity.java

Main activity file

src/main/res/layout/activity_lgfm_radio_app.xml

Layout for the main activity

Declaring Permissions

The following permissions must be declared to implement the LG FMRadio application. In the AndroidManifest.xml file of the sample application, the permission is declared as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="fmradio.lge.com.lgfmradioapp">
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.ACCESS_FM_RECEIVER"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />

 

Getting Ready

Importing the LG FMRadio package

In all classes that use the LG FMRadio APIs you need to import the LG FMRadio package as below.

import com.lge.fmradio.lgfmradio.LGFmRadio;

 

Creating the Main Activity

Main activity for sample application is created.

public class LGFmRadioApp extends Activity {…}

 

Implementing FM Radio Class

The layout of the sample application is created and the FM radio is turned on.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lgfm_radio_app);
        mRadioCallback = new LGFmradioCallback();
        mRadio = new LGFmRadio(this, mRadioCallback);
…
}

 

The RADIO ON/RADIO OFF button controls the FM radio.

mBtnRadioOnOff = (Button)findViewById(R.id.btn_radio_on_off);
        mBtnRadioOnOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mRadio.isRadioOn() == false) {
                    mRadio.RadioOn();
                } else {
                    mRadio.RadioOff();
                }
            }
});

 

The SEEK FORWARD button calls SeekForward() method and SEEK BACKWARD button calls SeekBackward() method.

mBtnSeekForward = (Button)findViewById(R.id.btn_seek_forward);
mBtnSeekForward.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mRadio.SeekForward();
    }
});
mBtnSeekBackward = (Button)findViewById(R.id.btn_seek_backward);
mBtnSeekBackward.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mRadio.SeekBackward();
    }
});

 

The TUNE button calls tune() method to change the channel to the specified frequency.

mBtnTune = (Button)findViewById(R.id.btn_tune;
mBtnTune.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mFrequency = mTunedFrequency;
        updateCurrentFrequency();
        mRadio.tune(mFrequency);
    }
});

 

The seekbar that changes frequency for the tune feature is created.

mSeekBarTune = (SeekBar)findViewById(R.id.seekbar_tune);
mSeekBarTune.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        mTunedFrequency = (progress * 10) + 8750;
        mTextTuneFreq.setText((double) mTunedFrequency / 100 + "MHz");
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
});

 

The SCAN button calls scan() method searches for all frequencies that can be listened to on FM radio.

mBtnScan = (Button)findViewById(R.id.btn_scan);
mBtnScan.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(mRadio.isScanning() == false) {
            mScannedList.clear();
            mRadio.scan();
        } else {
            mRadio.stopScanning();
        }
    }
});
…
mListScan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mFrequency = mScannedList.get(position);
        mRadio.tune(mFrequency);
    }
});

 

The MODE button is created to change the audio mode. The default mode is earphone mode and when the button is clicked, it is changed to speaker mode, mute and earphone mode again in that order.

mBtnAudioMode = (Button)findViewById(R.id.btn_audio_mode);
mBtnAudioMode.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(mCurrentAudioMode == LGFmRadio.AudioMode.AUDIO_MODE_EARPHONE) {
            mCurrentAudioMode = LGFmRadio.AudioMode.AUDIO_MODE_SPEAKER;
            mTextAudioMode.setText("SPEAKER");
        } else if(mCurrentAudioMode == LGFmRadio.AudioMode.AUDIO_MODE_SPEAKER) {
        mCurrentAudioMode = LGFmRadio.AudioMode.AUDIO_MODE_MUTE;
            mTextAudioMode.setText("MUTE");
        } else {
            mCurrentAudioMode = LGFmRadio.AudioMode.AUDIO_MODE_EARPHONE;
            mTextAudioMode.setText("EARPHONE");
        }
        mRadio.setAudioMode(mCurrentAudioMode);
    }
});

 

The toggle button for RDS on/off is created.

mBtnRdsOnOff = (Button)findViewById(R.id.btn_rds_on_off);
mBtnRdsOnOff.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(mRadio.isRdsOn() == false) {
            mRadio.RdsOn();
        } else {
            mRadio.RdsOff();
        }
    }
});

 

Implementing Callback Interface

The callback interface is implemented. The sample application receives the callback through an interface called ILGFmradioCallback.

class LGFmradioCallback implements LGFmRadio.ILGFmradioCallback
{
…    
public void onRadioOnOffNotify(boolean isOn, boolean isComplete) if(isComplete == true) {  ①
        if (isOn == true) {
            mRadio.tune(mFrequency);
            mBtnRadioOnOff.setText("RADIO OFF");
        } else if (isOn == false) {
            mBtnRadioOnOff.setText("RADIO ON");
        }
…
}
    public void onTuneNotify(boolean isComplete, int freq) {…} ②
    public void onSeekNotify(boolean isComplete, int freq) {…} ③
    public void onScanNotify(boolean isComplete, boolean found, int freq) {…} ④
    public void onRdsNotify(boolean isOn, boolean isComplete) {…} ⑤
    public void onRdsProgramServiceReceived(String sPS) {…} ⑥
    public void onRdsRadioTextReceived(String sRT, String sPT) {…} ⑦
    public void onRdsProgramIdentiReceived(String sPI) {…} ˆ
}

 

① onRadioOnOffNotify() is called if the FM radio is turned on/off through the RadioOn()/RadioOff().

② onTuneNotify() is called if the channel has been successfully changed to a specific frequency by the tune().

③ onSeekNotify() is called if the search for an available frequency has been completed by the SeekForward()/SeekBackward().

④ onScanNotify() is called to send results whilst the scan() is being executed.

⑤ onRdsNotify() is called if RdsOn()/RdsOff() has been called and completed.

⑥ onRdsProgramServiceReceived() is called to send Program Service to provide the application with RDS.

⑦ onRdsRadioTextReceived() is called to send RadioText and ProgramType to provide the application with RDS.

⑧ onRdsProgramIdentiReceived() is called to send PI code to provide the application with RDS.

 

 

 

Navigation