首 页 | 新 闻 | Symbian | Android| Windows Mobile | J2ME | 下载中心 | 游戏策划招聘与求职 | 购书指南 | 视频教程
您现在的位置: 开发视界 >> J2ME >> 可选包 >> 正文
定位API开发指南——例子:移动应用开发的定位和定位检测(5)
作者:姜 译    文章来源:诺基亚官方文档    更新时间:2006-12-26 16:42:18

此文为开发视界翻译转载者请注明出处(开发视界 www.sf.org.cn)否则追究法律责任
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类将返回相关对象的AddressInforGetspeed方法返回中断目标相对于地面对速度,以米每秒为单位。更多信息,参考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();

}

}

}
相关文章:
用J2ME在移动设备上实现动画
一些实用的图形用户界面方法
我的第一个J2ME程序
J2ME手机文件加密
编写Palm J2ME红外线“聊天”程序
解决J2ME联网时出现的中文乱码问题
J2ME专业手机游戏开发基础(一)
SVG(JSR 266)开发入门指南
 

站点地图 | 加入收藏 | 联系站长 | 广告服务 |
QQ:280529124  Tel:0592-8271361 辽ICP备05021703号