4.4.6 運行ProviderStatusListener類
1、創建ProviderStatusListener類文件
2、引入必要的文件包
package com.nokia.example.location.tourist.model;
3、創建類,添加兩個警告事件。
/**
* Listener interface for location providers status information.
*/
public interface ProviderStatusListener
{
/**
* A Notification event that location provider has been selected.
*/
public void providerSelectedEvent();
/**
* A Notification event about the first location update.
*/
public void firstLocationUpdateEvent();
}
4.4.7 運行TouristData類
1、創建TouristData類文件
2、引入必要的類
package com.nokia.example.location.tourist.model;
import Java.util.Enumeration;
import Javax.microedition.location.AddressInfo;
import Javax.microedition.location.Coordinates;
import Javax.microedition.location.Landmark;
import Javax.microedition.location.Location;
import Javax.microedition.location.LocationException;
import Javax.microedition.location.LocationListener;
import Javax.microedition.location.LocationProvider;
import Javax.microedition.location.ProximityListener;
import Javax.microedition.location.QualifIEdCoordinates;
import com.nokia.example.location.tourist.ui.TouristUI;
3、創建類TouristData,並設置運行LocationLiatener方法和ProximityListener方法。LocationListener代表與LocationProvider相關的一個位置接收端。ProximityListener作為一個接口,代表一個搜索已注冊坐標的事件的接收端。
更多信息,參考Location API。
/**
* Model class that handles LocationListeners and ProximityListeners events.
*/
public class TouristData implements LocationListener, ProximityListener
{
/** A Reference to Tourist UI Canvas */
private TouristUI touristUI = null;
/** Coordinate detection threshold radius in meters */
public static final float PROXIMITY_RADIUS = 100.0f;
/** Previous coordinates outside the distance threshold area (20m) */
private Coordinates prevCoordinates = null;
/** The first location update done. */
private boolean firstLocationUpdate = false;
private ProviderStatusListener statusListener = null;
4、為類創建結構。setLocationListener增加了一個LocationListener方法用於更新。
更多信息,參考Location API。
/**
* Construct instance of this model class.
*/
public TouristData(ProviderStatusListener listener)
{
statusListener = listener;
ConfigurationProvider config = ConfigurationProvider.getInstance();
// 1. Register LocationListener
LocationProvider provider = config.getSelectedProvider();
if (provider != null)
{
int interval = -1; // default interval of this provider
int timeout = 0; // parameter has no effect.
int maxage = 0; // parameter has no effect.
provider.setLocationListener(this, interval, timeout, maxage);
}
// 2. Register ProxymityListener to each Landmark found from the
// Landmark store.
ControlPoints cp = ControlPoints.getInstance();
Enumeration enumeration = cp.getLandMarks();
if (enumeration != null)
{
while (enumeration.hasMoreElements())
{
Landmark lm = (Landmark) enumeration.nextElement();
createProximityListener(lm.getQualifIEdCoordinates());
}
}
}
5、為TouristUI設置一個設置方法。
/**
* Setter method to TouristUI reference.
*
* @param ui -
* Reference to TouristUI.
*/
public void setTouristUI(TouristUI ui)
{
touristUI = ui;
}
6、創建方法,為定位提供增加一個新的ProximityListener。當發現一個指定的坐標的時候,addProximityListener增加一個ProximityListener。
/**
* Adds new ProximityListener to the location provider. This method is
* called when constructing instance of this class and when a new landmark
* is saved by using LandmarkEditorUI.
*
* @param coordinates
*/
public void createProximityListener(Coordinates coordinates)
{
try
{
LocationProvider.addProximityListener(this, coordinates,
PROXIMITY_RADIUS);
}
catch (LocationException e)
{
// Platform does not have resources to add a new listener
// and coordinates to be monitored or does not support
// proximity monitoring at all
}
}
7、從LocationListener開始啟動一個新的線程,阻止堵塞。Location類代表了標准的位置信息。無論Location類實例表示一個有效的位置坐標還是無效的坐標,甚至不能返回經度和緯度,IsValid方法都將返回。AddressInfor類存儲了一個位置信息的文本信息,getAddress類將返回相關對象的AddressInfor。Getspeed方法返回中斷目標相對於地面對速度,以米每秒為單位。更多信息,參考Location API。
/**
* locationUpdated event from LocationListener interface. This method starts
* a new thread to prevent blocking, because listener method MUST return
* quickly and should not perform any extensive processing.
*
* Location parameter is set final, so that the anonymous Thread class can
* Access the value.
*/
public void locationUpdated(LocationProvider provider,
final Location location)
{
// First location update arrived, so we may show the UI (TouristUI)
if (!firstLocationUpdate)
{
firstLocationUpdate = true;
statusListener.firstLocationUpdateEvent();
}
if (touristUI != null)
{
new Thread()
{
public void run()
{
if (location != null && location.isValid())
{
AddressInfo address = location.getAddressInfo();
QualifiedCoordinates coord = location.getQualifIEdCoordinates
();
float speed = location.getSpeed();
touristUI.setInfo(address, coord, speed);
touristUI.setProviderState("Available");
touristUI.repaint();
}
else
{
touristUI.setProviderState("Not valid location data");
touristUI.repaint();
}
}
}.start();
}
}
8、通過LcoationListener方法創建狀態變更時間,啟動新的線程阻止阻塞。
/**
* providerStateChanged event from LocationListener interface. This method
* starts a new thread to prevent blocking, because listener method MUST
* return quickly and should not perform any extensive processing.
*
* newState parameter is set final, so that the anonymous Thread class can
* Access the value.
*/
public void providerStateChanged(LocationProvider provider,
final int newState)
{
if (touristUI != null)
{
new Thread()
{
public void run()
{
switch (newState) {
case LocationProvider.AVAILABLE:
touristUI.setProviderState("Available");
break;
case LocationProvider.OUT_OF_SERVICE:
touristUI.setProviderState("Out of service");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
touristUI
.setProviderState("Temporarily unavailable");
break;
default:
touristUI.setProviderState("Unknown");
break;
}
touristUI.repaint();
}
}.start();
}
}
9、為ProximityListener創建proximity事件,只有當用戶輸入注冊的坐標的時候調用。GetAddersInfor方法返回與Location對象相關的AddressInfor信息。
/**
* proximity event from ProximityListener interface. The listener is called
* only once when the terminal enters the proximity of the registered
* coordinates. That's why no this method should not need to start a new
* thread.
*/
public void proximityEvent(Coordinates coordinates, Location location)
{
if (touristUI != null)
{
touristUI.setProviderState("Control point found!");
Landmark lm = ControlPoints.getInstance().findNearestLandMark(
coordinates);
// landmark found from landmark store
if (lm != null)
{
touristUI.setInfo(lm.getAddressInfo(), lm
.getQualifIEdCoordinates(), location.getSpeed());
}
// landmark not found from the landmark store, this should not never
// happen!
else
{
touristUI.setInfo(location.getAddressInfo(), location
.getQualifIEdCoordinates(), location.getSpeed());
}
touristUI.repaint();
}
}
10 通過ProximityListener創建狀態更改事件,用來說明狀態已經改變。
/**
* monitoringStateChanged event from ProximityListener interface. NotifIEs
* that the state of the proximity monitoring has changed. That's why this
* method should not need to start a new thread.
*/
public void monitoringStateChanged(boolean isMonitoringActive)
{
if (touristUI != null)
{
if (isMonitoringActive)
{
// proximity monitoring is active
touristUI.setProximityState("Active");
}
else
{
// proximity monitoring can't be done currently.
touristUI.setProximityState("Off");
}
touristUI.repaint();
}
}
}