4.4 開發Tourist Route MIDLet
該部分通過定位API開發實例應用程序。注意,本節的內容只是演示了一種實現功能的方法。可以利用這些作為基礎,通過自己的知識用不同的方法開發自己的程序。
工程項目環境由S60或者serIEs 40 SDK中的命令行組成。
1、創建TouristMIDLet類文件。
2、引入必要的類。
package com.nokia.example.location.tourist;
import Javax.microedition.lcdui.Display;
import Javax.microedition.midlet.MIDlet;
import Javax.microedition.midlet.MIDletStateChangeException;
import com.nokia.example.location.tourist.model.ConfigurationProvider;
import com.nokia.example.location.tourist.model.ProviderStatusListener;
import com.nokia.example.location.tourist.model.TouristData;
import com.nokia.example.location.tourist.ui.MessageUI;
3、將TouristMIDlet設置為擴展MIDLet。運行ProviderStatusListener。創建結構。
/**
* Tourist Route MIDlet class.
*/
public class TouristMIDlet extends MIDlet implements ProviderStatusListener
{
/** A static reference to Display object. */
private static Display display = null;
/** A Reference to TouristData. */
private TouristData data = null;
/** Lock object */
private Object mutex = new Object();
public TouristMIDlet()
{
super();
}
4、定義應用程序必須的3個MIDLet方法
protected void startApp() throws MIDletStateChangeException
{
display = Display.getDisplay(this);
if (ConfigurationProvider.isLocationApiSupported())
{
ConfigurationProvider.getInstance().autoSearch(this);
}
else
{
MessageUI.showApiNotSupported();
}
}
protected void pauseApp()
{
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException
{
}
5、創建一個獲取顯示對象的方法
/**
* Getter method for Display reference.
*
* @return reference to Display object.
*/
public static Display getDisplay()
{
return display;
}
6、創建說明定位選擇的方法
/**
* Event indicating location provider is selected. MIDlet use may therefore
* begin.
*
* @see
com.nokia.example.location.tourist.model.ProviderSelectedListener#providerSelected
Event()
*/
public void providerSelectedEvent()
{
// Attempt to acquire the mutex
synchronized (mutex)
{
// Start scanning location updates. Also set the TouristData
// reference data.
MessageUI.showLocationProviderState();
// Inform the user that MIDlet is looking for location data.
data = new TouristData((ProviderStatusListener) this);
}
}
7、創建方法,說明第一個定位服務的更新已經完成
/**
* Event indication about the first location update. This method sets
* Tourist UI visible. By using mutex object, we ensure TouristaData (data)
* is created on providerSelectedEvent.
*
* @see
com.nokia.example.location.tourist.model.ProviderStatusListener#firstLocationUpdat
eEvent()
*/
public void firstLocationUpdateEvent()
{
// Attempt to acquire the mutex
synchronized (mutex)
{
TouristUI ui = new TouristUI(data);
data.setTouristUI(ui);
display.setCurrent(ui);
}
}
}