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

此文为开发视界翻译转载者请注明出处(开发视界 www.sf.org.cn)否则追究法律责任
4.4.3
运行Utils类

1、创建Utils类文件

2、将类加入com.nokia.example.location.tourist包中

3、创建Utils类,编写字符串显示的代码。

/**

* A container class for general purpose utility method(s).

*/

public class Utils

{

/**

* Creates a String presentation of double value that is formatted to have a

* certain number of decimals. Formatted output value does not include any

* rounding rules. Output value is just truncated on the place that is

* defined by received decimals parameter.

*

* @param value -

* double value to be converted.

* @param decimals -

* number of decimals in the String presentation.

* @return a string representation of the argument.

*/

public static String formatDouble(double value, int decimals)

{

String doubleStr = "" + value;

int index = doubleStr.indexOf(".") != -1 ? doubleStr.indexOf(".")

: doubleStr.indexOf(",");

// Decimal point can not be found...

if (index == -1) return doubleStr;

// Truncate all decimals

if (decimals == 0)

{

return doubleStr.substring(0, index);

}

int len = index + decimals + 1;

if (len >= doubleStr.length()) len = doubleStr.length();

double d = Double.parseDouble(doubleStr.substring(0, len));

return String.valueOf(d);

}

}

4.4.4 运行ConfigurationProvider

1、创建ConfigurationProvider文件

2、引入必要的类

package com.nokia.example.location.tourist.model;

import javax.microedition.location.Criteria;

import javax.microedition.location.LocationException;

import javax.microedition.location.LocationProvider;

import javax.microedition.location.Orientation;

import com.nokia.example.location.tourist.ui.ProviderQueryUI;

3、创建处理定位搜索的类

/**

* Model class that handles location providers search.

*/

public class ConfigurationProvider

{

private static ConfigurationProvider INSTANCE = null;

/** Array of free Criterias. */

private static Criteria[] freeCriterias = null;

/** String array of free criteria names. */

private static String[] freeCriteriaNames = null;

/** Array of Criterias that may cause costs */

private static Criteria[] costCriterias = null;

/** String array of non-free criteria names. */

private static String[] costCriteriaNames = null;

/** Reference to ProviderQueryUI viewer class. */

private ProviderQueryUI queryUI = null;

/** Selected criteria */

private Criteria criteria = null;

/** Selected location provider */

private LocationProvider provider = null;

4、编写静态模块创建参数结构。用来选择定位服务的Criteria类是已经确定的。关于这个类和方法的更多信息,参考API定位。

/*

* Static block that constructs the content of free and non-free Criterias.

* This code block is performed before the constructor.

*/

static

{

// 1. Create pre-defined free criterias

freeCriterias = new Criteria[2];

freeCriteriaNames = new String[2];

Criteria crit1 = new Criteria();

crit1.setHorizontalAccuracy(25); // 25m

crit1.setVerticalAccuracy(25); // 25m

crit1.setPreferredResponseTime(Criteria.NO_REQUIREMENT);

crit1.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);

crit1.setCostAllowed(false);

crit1.setSpeedAndCourseRequired(true);

crit1.setAltitudeRequired(true);

crit1.setAddressInfoRequired(true);

freeCriterias[0] = crit1;

freeCriteriaNames[0] = "High details, cost not allowed";

Criteria crit2 = new Criteria();

crit2.setHorizontalAccuracy(Criteria.NO_REQUIREMENT);

crit2.setVerticalAccuracy(Criteria.NO_REQUIREMENT);

crit2.setPreferredResponseTime(Criteria.NO_REQUIREMENT);

crit2.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);

crit2.setCostAllowed(false); // allowed to cost

crit2.setSpeedAndCourseRequired(false);

crit2.setAltitudeRequired(false);

crit2.setAddressInfoRequired(false);

freeCriterias[1] = crit2;

freeCriteriaNames[1] = "Low details and power consumption, cost not

allowed";

编写静态模块构建非免费参数

// 2. Create pre-defined cost criterias

costCriterias = new Criteria[3];

costCriteriaNames = new String[3];

Criteria crit3 = new Criteria();

crit3.setHorizontalAccuracy(25); // 25m

crit3.setVerticalAccuracy(25); // 25m

crit3.setPreferredResponseTime(Criteria.NO_REQUIREMENT);

crit3.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT);

crit3.setCostAllowed(true);

crit3.setSpeedAndCourseRequired(true);

crit3.setAltitudeRequired(true);

crit3.setAddressInfoRequired(true);

costCriterias[0] = crit3;

costCriteriaNames[0] = "High details, cost allowed";

Criteria crit4 = new Criteria();

crit4.setHorizontalAccuracy(500); // 500m

crit4.setVerticalAccuracy(Criteria.NO_REQUIREMENT);

crit4.setPreferredResponseTime(Criteria.NO_REQUIREMENT);

crit4.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT);

crit4.setCostAllowed(true);

crit4.setSpeedAndCourseRequired(true);

crit4.setAltitudeRequired(true);

crit4.setAddressInfoRequired(false);

costCriterias[1] = crit4;

costCriteriaNames[1] = "Medium details, cost allowed";

// Least restrictive criteria (with default values)

Criteria crit5 = null;

costCriterias[2] = crit5;

costCriteriaNames[2] = "Least restrictive criteria";

}

5、为类创建结构

/**

* Private constructor to force using getInstance() method.

*/

private ConfigurationProvider()

{

queryUI = new ProviderQueryUI();

}

6、创建方法,为类提供一个实例。

/**

* Provide singleton instance of this class.

*

* @return static instance of this class.

*/

public static ConfigurationProvider getInstance()

{

if (INSTANCE == null)

{

// Enable use of this class when Location API is supported.

if (isLocationApiSupported())

{

INSTANCE = new ConfigurationProvider();

}

else

{

INSTANCE = null;

}

}

return INSTANCE;

}

7、创建方法,检测是否支持API

/**

* Checks whether Location API is supported.

*

* @return a boolean indicating is Location API supported.

*/

public static boolean isLocationApiSupported()

{

String version = System.getProperty("microedition.location.version");

return (version != null && !version.equals("")) ? true : false;

}

public LocationProvider getSelectedProvider()

{

return provider;

}

8、使用预先定义的参数,创建搜索定位服务的方法。getInstance是一个内置的方法,根据已经确定的参数运行LocationProvider。当API定位发生错误的时候,程序跳转至LocationExceptiongetOrientation方法返回最终的定位源。

关于该类的更所信息,参考API定位。

/**

*

* @param listener -

* a event listener that listens ProviderStatusLisneter events.

*/

public void autoSearch(ProviderStatusListener listener)

{

try

{

for (int i = 0; i < freeCriterias.length; i++)

{

criteria = freeCriterias[i];

provider = LocationProvider.getInstance(criteria);

if (provider != null)

{

// Location provider found, send a selection event.

listener.providerSelectedEvent();

return;

}

}

if (queryUI.confirmCostProvider())

{

for (int i = 0; i < costCriterias.length; i++)

{

criteria = costCriterias[i];

provider = LocationProvider.getInstance(criteria);

if (provider != null)

{

// Location provider found, send a selection event.

listener.providerSelectedEvent();

return;

}

}

}

else

{

queryUI.showNoFreeServiceFound();

}

}

catch (LocationException le)

{

queryUI.showOutOfService();

}

}

public Orientation getOrientation()

{

try

{

return Orientation.getOrientation();

}

catch (LocationException e)

{

return null;

}

}

9、创建方法,判断是否支持Oritation

/**

* Tells whether orientation is supported.

*

* @return a boolean indicating is orientation supported.

*/

public boolean isOrientationSupported()

{

try

{

// Test whether Orientation instance can be obtained.

Orientation.getOrientation();

return true;

}

catch (LocationException e)

{

return false;

}

}

}
相关文章:
在无线J2ME设备上实现http传输
用MMAPI开发手机摄像头程序
一些实用的图形用户界面方法
J2ME手机文件加密
SVG(JSR 266)开发入门指南
单例模式的RMS访问类
j2me多媒体开发
J2ME程序与Servlet通讯访问Access数据库
 

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