【问题标题】:Adding gauge in location finder class in j2me在 j2me 的位置查找器类中添加仪表
【发布时间】:2014-05-15 10:08:04
【问题描述】:

我正在开发一个 j2me 应用程序,其中包含一个使用 GPS 查找移动设备位置的类。我需要在调用位置提供程序 API 并找到位置时包含仪表。我是 j2me 的新手,所以仍然不清楚包含所有概念。我在下面粘贴我的代码。请帮助我完成此操作。提前致谢..

package org.ets.utils;
import javax.microedition.lcdui.*;
import javax.microedition.location.*;
import javax.microedition.io.*;
import java.io.*;
import org.ets.midlet.ETS_infozech;
import javax.microedition.midlet.*;


public class Locfinder  {






public Locfinder(ETS_infozech midlet)
{
    this.midlet = midlet;


}

public static String ex()
    {

        try {


                     checkLocation();
                    } catch (Exception ex)
                    {
                        ex.printStackTrace();

        }
        //System.out.println(string);
        return string;

    }

public static void  checkLocation() throws Exception
{

    Location l;
    LocationProvider lp;
    Coordinates c;
    // Set criteria for selecting a location provider:
    // accurate to 500 meters horizontally
    Criteria cr= new Criteria();
    cr.setHorizontalAccuracy(500);

    // Get an instance of the provider
   lp= LocationProvider.getInstance(cr);

    //Request the location, setting a one-minute timeout
    l = lp.getLocation(60);
    c = l.getQualifiedCoordinates();



    if(c != null ) {
      // Use coordinate information
      double lat = c.getLatitude();
      double lon = c.getLongitude();
      string = " LAT-" + lat + " LONG-" + lon;

    } 




}

}

【问题讨论】:

    标签: java-me geolocation midp lcdui gauge


    【解决方案1】:

    您无法将Gauge 链接到某个任务。

    您必须手动为Gauge 设置值。因此,您将创建一个Gauge 并将其添加到您的Form。然后启动您的代码来执行查找。

    在您的代码行之间,您可以添加myGauge.setValue(some_value); 来增加指标。

    当然,当大部分任务都包含在一行代码中时,这会变得很困难,例如lp.getLocation(60);。 我认为,在这种情况下,我会创建一个 Thread,它会在 60 秒内自动增加 Gauge 上的值,但可以通过手动设置来停止/覆盖。

    class Autoincrementer implements Runnable {
    
    private boolean running;
    private Gauge gauge;
    private int seconds;
    private int secondsElapsed;
    
     public Autoincrementer(Gauge gauge) {
      this.gauge = gauge;
      this.seconds = gauge.getMaxValue();
      this.running = true;
      this.secondsElapsed = 0;
     }
    
     public void run() {
    
      if (running) {
       secondsElapsed++;
       gauge.setValue(secondsElapsed);
       if (secondsElapsed>=gauge.getMaxValue()) running = false; // Stop the auto incrementing
       try {
        Thread.sleep(1000); // Sleep for 1 second
       } catch (Exception e) {}
      }
     }
    
     public void stop() {
      running = false;
     }
    
    }
    

    然后您将创建一个 Gauge 并将其添加到您的 Form

    myGauge = new Gauge("Process", false, 60, 0);
    myForm.append(myGauge);
    

    然后开始自增。

    myIncrementer = new Autoincrementer(myGauge);
    new Thread(myIncrementer).start();
    

    然后调用你的查找代码。

    checkLocation();
    

    在您的查找代码中,添加代码以停止自动递增并将 Gauge 对象设置为 100%,如果查找成功(意味着在超时之前)。

    myIncrementer.stop();
    myGauge.setValue(60);
    

    【讨论】:

      【解决方案2】:

      LWUIT 1.5 可以在这方面为您提供帮助。不确定您正在使用的 Location API。 但是您将使用 LWUIT 1.5 获得 Gauge。使用 Lwuit 而不是 LCDUI。

      http://www.oracle.com/technetwork/java/javame/javamobile/download/lwuit/index.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多