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

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

編輯:J2ME

4.4.10 運行MessageUI

1、建MessageUI類文件

2、入必要的類。

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

import Javax.microedition.lcdui.Alert;

import Javax.microedition.lcdui.AlertType;

import Javax.microedition.lcdui.Gauge;

import com.nokia.example.location.tourist.TouristMIDlet;

3、建MessageUI類

/**

* VIEwer class containing a general suppose Alert notification(s).

*/

public class MessageUI

{

4、建方法,對於不支持API定位的時候發出警告。

/**

* Shows a alert that Location API is not supported.

*/

public static void showApiNotSupported()

{

Alert alert = new Alert("Information",

"Device do not support Location API", null, AlertType.INFO);

TouristMIDlet.getDisplay().setCurrent(alert);

}

5、建方法,不可獲取位置信息的時候發出警告。

/**

* Shows a alert that Location data is not available.

*/

public static void showLocationDataNotAvailable()

{

Alert alert = new Alert("Information",

"Location data is not yet available.", null, AlertType.INFO);

TouristMIDlet.getDisplay().setCurrent(alert);

}

6、建顯示定位提供狀態的對話。

/**

* Show a status dialog informing about state of location provider.

*/

public static void showLocationProviderState()

{

Gauge indicator = new Gauge(null, false, 50, 1);

indicator.setValue(Gauge.CONTINUOUS_RUNNING);

Alert alert = new Alert("Information",

"Please wait, looking for location data....", null,

AlertType.INFO);

alert.setIndicator(indicator);

TouristMIDlet.getDisplay().setCurrent(alert);

}

}

4.4.11 運行PitchRollUI

1、建PitchRollUI類

2、入必要的類

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

import Javax.microedition.lcdui.Canvas;

import Javax.microedition.lcdui.Command;

import Javax.microedition.lcdui.CommandListener;

import Javax.microedition.lcdui.Displayable;

import Javax.microedition.lcdui.Graphics;

import Javax.microedition.location.OrIEntation;

import com.nokia.example.location.tourist.TouristMIDlet;

import com.nokia.example.location.tourist.model.ConfigurationProvider;

3、建PitchRollUI類,將其設置為Canvas擴展,並運行CommandListener和Runable。定義所需要的命令和常量,創建結構。

/**

* Viewer class that renders orIEntations pitch and roll values to meters.

*/

public class PitchRollUI extends Canvas implements CommandListener, Runnable

{

private CompassUI compass;

private float pitch = Float.NaN;

private float roll = Float.NaN;

/** Flag telling is the compass update thread active */

private boolean threadActive = false;

/** Sleep 1000ms during each compass update */

private final long SLEEPTIME = 1000;

private Command compassCmd = new Command("Compass", Command.BACK, 1);

public PitchRollUI(CompassUI compass)

{

this.compass = compass;

addCommand(compassCmd);

setCommandListener(this);

}

4、建方法,控制commpass線程的更新。

/**

* VM calls this method immediately prior to this Canvas being made visible

* on the display.

*/

protected void showNotify()

{

// Actives compass update thread.

threadActive = true;

new Thread(this).start();

}

/**

* VM calls this method shortly after the Canvas has been removed from the

* display.

*/

protected void hideNotify()

{

// Stops the thread.

threadActive = false;

}

5、該類創建run方法,需要更新角度、長度和長度並覆蓋canvas。更多信息參考Location API。

/**

* run method from Runnable interface. Updates azimuth, pitch and roll

* values and repaints the canvas.

*

* If OrIEntation is supported on the terminal, support level may be either

* 2D or 3D depending on the compass sensor. If the sensor providers only

* compass azimuth, pitch and roll values are Float.NaN.

*/

public void run()

{

// Run this thread until this displayable is not visiable.

// See also hideNotify() method.

while (threadActive)

{

Orientation orIEntation = ConfigurationProvider.getInstance()

.getOrIEntation();

if (orIEntation != null)

{

pitch = orIEntation.getPitch();

roll = orIEntation.getRoll();

}

repaint();

try

{

// Pause this thread for a secord before next update.

Thread.sleep(SLEEPTIME);

}

catch (InterruptedException e)

{

}

}

}

6、建方法,引導canvas。

/**

* Renders the canvas.

*

* @param g -

* the Graphics object to be used for rendering the Canvas

*/

protected void paint(Graphics g)

{

// clean up canvas

g.setColor(255, 255, 255);

g.fillRect(0, 0, getWidth(), getHeight());

paintPitch(g);

paintRoll(g);

}

7、建方法,創建標尺。

/**

* Draws a customized meter with a scale.

*/

protected void drawMeter(Graphics g, int min, int max, int smallStep,

int largeStep, int baseline, int margin)

{

double position;

double scale = 100.0; // scale value to minimize rounding error

int x;

g.setColor(0x0000ff);

g.drawLine(margin, baseline, getWidth(), baseline);

for (int i = min; i <= max; i += smallStep)

{

position = (((i + max) * scale * (getWidth() - margin)) / (2 * max))

/ scale;

x = margin + (int) position;

g.drawLine(x, baseline - 3, x, baseline + 3);

}

for (int i = min; i <= max; i += largeStep)

{

position = (((i + max) * scale * (getWidth() - margin)) / (2 * max))

/ scale;

x = margin + (int) position;

g.drawLine(x, baseline - 5, x, baseline + 5);

}

}

8、建方法,覆蓋高度原值,賦予新的高度值。

/**

* Paint pitch meter and place the current pitch value to the meter.

*

* @param g -

* the Graphics object to be used for rendering the Canvas

*/

protected void paintPitch(Graphics g)

{

int baseline = 10;

int textAreaWidth = 50;

double position;

g.setColor(0x000000);

g.drawString("Pitch", 0, baseline - 5, Graphics.TOP | Graphics.LEFT);

g.drawString("-90", textAreaWidth + 1, baseline, Graphics.TOP

| Graphics.LEFT);

g.drawString("0", textAreaWidth + (getWidth() - textAreaWidth) / 2,

baseline, Graphics.TOP | Graphics.HCENTER);

g.drawString("+90", getWidth(), baseline, Graphics.TOP

| Graphics.RIGHT);

drawMeter(g, -90, 90, 10, 30, baseline, textAreaWidth);

// Draw the marker only if terminals pitch is available.

if (pitch != Float.NaN)

{

position = (((pitch + 90.0) * 100 * (getWidth() - textAreaWidth)) / (2

* 90)) / 100;

g.setColor(0x000000);

g.fillRect(textAreaWidth + (int) position - 3, baseline - 2, 5, 5);

}

}

9、建方法,覆蓋長度並賦予新的長度值。

/**

* Paint roll meter and place the current pitch value to the meter.

*

* @param g -

* the Graphics object to be used for rendering the Canvas

*/

protected void paintRoll(Graphics g)

{

int baseline = 40;

int textAreaWidth = 50;

double position;

g.setColor(0x000000);

g.drawString("Roll", 0, baseline - 5, Graphics.TOP | Graphics.LEFT);

g.drawString("-180", textAreaWidth + 1, baseline, Graphics.TOP

| Graphics.LEFT);

g.drawString("0", textAreaWidth + (getWidth() - textAreaWidth) / 2,

baseline, Graphics.TOP | Graphics.HCENTER);

g.drawString("+180", getWidth(), baseline, Graphics.TOP

| Graphics.RIGHT);

drawMeter(g, -180, 180, 15, 60, baseline, textAreaWidth);

// Draw the marker only if terminals roll is available.

if (roll != Float.NaN)

{

position = (((roll + 180.0) * 100 * (getWidth() - textAreaWidth)) / (2

* 180)) / 100;

g.setColor(0x000000);

g.fillRect(textAreaWidth + (int) position - 3, baseline - 2, 5, 5);

}

}

10、建事件,監控按鍵的輸入。

/**

* Event indicating when a command button is pressed.

*

* @see Javax.microedition.lcdui.CommandListener#commandAction

(Javax.microedition.lcdui.Command,

* Javax.microedition.lcdui.Displayable)

*/

public void commandAction(Command command, Displayable d)

{

if (command == compassCmd)

{

TouristMIDlet.getDisplay().setCurrent(compass);

}

}

}

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