程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> 定位API開發指南——例子:移動應用開發的定位和定位檢測(3)

定位API開發指南——例子:移動應用開發的定位和定位檢測(3)

編輯:J2ME


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定位發生錯誤的時候,程序跳轉至LocationException。getOrIEntation方法返回最終的定位源。

關於該類的更所信息,參考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;

}

}

}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved