当前位置: 首页 > 知识库问答 >
问题:

位置管理器在没有internet的情况下无法工作

艾子石
2023-03-14
public class NativeGeolocation extends Plugin {
  public long maximumAge = 1000 * 30; // ms
  public long timeout    = 1000 * 30; // ms
  public Location lastPosition = null; 
  public static final String ACTION_GETCURRENTPOSITION="getCurrentPosition";
  protected String callbackId = null;

  @Override
  public PluginResult execute(String action, JSONArray data, String callbackId)
  {
    JSONObject options = data.optJSONObject(0);
    Log.i("Myactivity","options : "+options);
    this.timeout    = timeout;

    this.callbackId = callbackId;
    Log.i("Myactivity","callbackId : "+this.callbackId);
    PluginResult result = new PluginResult(Status.NO_RESULT, callbackId);
    result.setKeepCallback(true);
    final LocationManager locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();

    String provider = locationManager.getBestProvider(criteria,false);
    if (ACTION_GETCURRENTPOSITION.equals(action)) {
      Log.i("Myactivity","inside getcurrentposition action");
      Location lastKnownLocation = locationManager.getLastKnownLocation(provider);
      lastPosition = lastKnownLocation;
      Log.i("Myactivity","last location "+lastKnownLocation);
      if ((null != lastKnownLocation) && lastKnownLocation.getTime() + this.maximumAge > new Date().getTime()) {
        Log.i("Myactivity","inside b4 gotLocation");
        gotLocation(lastKnownLocation);
      } else {
        ctx.runOnUiThread(new RunnableLocationListener(this, callbackId, locationManager, provider));
      }
    } else {
      error(new PluginResult(Status.INVALID_ACTION), callbackId);
    }
    return result;
  }  

  public void gotLocation (Location location) {
    Log.i("Myactivity","inside  gotLocation");
    try {
      Log.i("Myactivity","inside  try");
      JSONObject geoposition = new JSONObject();
      JSONObject coords = new JSONObject();
      coords.put("latitude",         location.getLatitude());
      coords.put("longitude",        location.getLongitude());
      coords.put("altitude",         location.getAltitude());
      coords.put("accuracy",         location.getAccuracy());
      coords.put("altitudeAccuracy", null);
      coords.put("heading",          null);
      coords.put("speed",            location.getSpeed());
      geoposition.put("coords",    coords);
      geoposition.put("timestamp", location.getTime());
      geoposition.put("provider",  location.getProvider());
      geoposition.put("lastPos",  lastPosition);
      success(new PluginResult(Status.OK, geoposition), callbackId);
    } catch (JSONException jsonEx) {
      error(new PluginResult(Status.JSON_EXCEPTION), callbackId);
    }
  }

  protected String getBestProvider (LocationManager locationManager) {
    String provider = LocationManager.PASSIVE_PROVIDER;
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
      provider = LocationManager.GPS_PROVIDER;
    }
    else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
      provider = LocationManager.NETWORK_PROVIDER;
    }
    return provider;
  }

  class RunnableLocationListener extends Thread {
    protected final NativeGeolocation plugin;
    protected final LocationManager locationManager;
    protected final String provider;
    protected final String callbackId;
    public Boolean ended = null;
    public RunnableLocationListener (NativeGeolocation plugin, String callbackId, LocationManager locationManager, String provider) {
      Log.i("Myactivity","inside  runnabl");
      this.plugin = plugin;
      this.locationManager = locationManager;
      this.provider = provider;
      this.callbackId = callbackId;
    }

    public void run () {
      ended = false;
      PluginResult result = null;
      final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged (Location location) {
            Log.i("Myactivity","calling getlocation again1");
          if ( false == ended ) {
            Log.i("Myactivity","calling getlocation again2");
            plugin.gotLocation(location);
            locationManager.removeUpdates(this);
          }
        }
        public void onStatusChanged (String provider, int status, Bundle extras) {
          Log.i("Myactivity","inside onStatus Changed");
    }
        public void onProviderEnabled(String provider) {
          Log.i("Myactivity","inside onProvider Enabled");
    }
        public void onProviderDisabled(String provider) {
          Log.i("Myactivity","inside onProvider Disabled");
    }
      };
      locationManager.requestLocationUpdates(provider,1, 10, locationListener);//1 minutes and 10 meters
      Thread timeouter = new Thread() {
        public void run () {
          try {
            Thread.sleep(plugin.timeout);
            ended = true;
            locationManager.removeUpdates(locationListener);
            plugin.error(new PluginResult(Status.ERROR, "timeout"), callbackId);
          } catch (java.lang.InterruptedException ex) {
            error(new PluginResult(Status.JSON_EXCEPTION), callbackId);
          }
        }
      };
      timeouter.start();
    }
  }
}

共有1个答案

贝成业
2023-03-14
LocationProvider gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
if(gpsProvider != null){
   locationManager.requestLocationUpdates(gpsProvider.getName(), 0, 0, this);
}


Boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Location lastLocation = null, gpsLocation = null;
if (isGPSEnabled) 
   gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
 类似资料:
  • 当我在Jenkins从属上运行以下代码段时,它可以工作。但是当它在没有BASH的docker容器中执行时,就不会设置“$test”。 https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-withenv-code-set-environment-variables

  • 问题内容: 我正在尝试在办公室笔记本电脑上安装JDK,但是它说我需要管理员权限。我只能在工作中使用自己的帐户。 没有管理员权限的情况下如何安装Java开发套件? 问题答案: 这是一种在没有管理员特权或没有管理员密码的情况下安装Java的解决方法。为此,您需要安装不需要管理员特权的cygwin。在utils中,确保您选择cabextract.exe进行安装。 启动Cygwin Bash外壳。 键入c

  • 我试着去看其他的代码例子,但是它们和我的代码比较相似,但是我的应用程序仍然会因为同样的错误而崩溃。 这是我使用firebase文档的指导方针编写的代码: 上面的代码正在将otp发送到给定的号码,但是它崩溃了,并且cat-log显示了上面提到的错误。

  • 我面临的问题在我的泽西岛邮政API当我没有发送内容长度从邮递员。 我们知道,默认情况下,postman发送的是content-length(隐藏的头),但我不发送。 2020-12-09 14:51:36.951错误27356---[nio-8082-exec-1]O.A.C.C.C.[.[.[/].[dispatcherServlet]:servlet[dispatcherServlet]的se

  • 如果我有以下骆驼路线并发送 udp 消息,则不会将任何内容打印到 stdout: 但是,如果我添加解码器,我可以看到 udp 消息打印在标准输出上 有人可以解释一下为什么默认解码器不会将消息传递到模拟endpoint。它正在等待EOF类型的信号吗? 我正在使用以下方式发送消息: Apache camel netty页面解释说:

  • 谁能帮我一下我做错了什么?或者是否有其他方法满足要求。