如果gps打开,位置管理器不工作。如果gps是,它的工作正常

时间:2016-02-04 10:39:46

标签: android gps android-location

在我的代码中加载布局时,它会获取gps坐标。这是我的示例代码。如果GPS关闭,它可以正常工作。如果我打开gps它没有加载gps坐标。我需要在打开GPS时获得用户gps坐标。那有什么问题呢。我需要改变的地方。抱歉我的英文。

 dialog = new ProgressDialog(FXPage.this);
  dialog.show();
  dialog.setMessage("Getting Coordinates");

  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  if (locationManager
          .isProviderEnabled(LocationManager.GPS_PROVIDER)) {
      locationManager.requestLocationUpdates(
      LocationManager.GPS_PROVIDER, 100000000,
      1, this);
      } else if (locationManager
              .isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
      locationManager.requestLocationUpdates(
      LocationManager.NETWORK_PROVIDER, 100000000,
      1, this);
      }
      else {
          dialog.dismiss();

          Toast.makeText(getApplicationContext(), "Enable Location", Toast.LENGTH_LONG).show();
      }
 protected void refresh() {

           super.onResume();
           this.recreate();

        }

     @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            dialog.show();
            latitude = location.getLatitude();
            longitude =location.getLongitude();
            if (latitude != 0 && longitude != 0){

            edittext6.setText(location.getLatitude()+","+location.getLongitude());

            dialog.dismiss();
            }
        }

     @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }


        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }


        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

2 个答案:

答案 0 :(得分:0)

  1. GPS不能在屋顶下工作。
  2. 如果可用,您可以先从网络提供商处获取位置更新。
  3. 如果N / A只是从GPS请求并启动CountDownTimer
  4. 现在计时器到期后,您可以检查该位置是否仍为空,然后提醒用户“无法获取位置更新”并停止GPS位置更新请求。
  5. 要避免上述所有问题,您可能只想使用FusedLocation Api,它比以前的LocationApi更好用。 查看FusedLocation api的this链接。

答案 1 :(得分:0)

这部分代码似乎很奇怪:

 @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        dialog.show();
        latitude = location.getLatitude();
        longitude =location.getLongitude();
        if (latitude != 0 && longitude != 0){

        edittext6.setText(location.getLatitude()+","+location.getLongitude());

        dialog.dismiss();
        }
    }

基本上您正在显示然后立即解除对话框。也许你应该在显示坐标后使用计时器来关闭对话框。

以下是获得单个位置更新的一些好建议。

首先检查GPS是否已启用,这将使您的工作流程更加强大:

final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

    if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
        buildAlertMessageNoGps();
    }

  private void buildAlertMessageNoGps() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                   startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
               }
           })
           .setNegativeButton("No", new DialogInterface.OnClickListener() {
               public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                    dialog.cancel();
               }
           });
    final AlertDialog alert = builder.create();
    alert.show();
}

然后在向 LocationManager 询问新的位置更新之前,我会检查是否有足够好的“最后已知位置”(这显然取决于您需要的精度)。 例如,您可以遍历每个位置提供程序以查找最及时和准确的最后已知位置,如下所示:

List<String> matchingProviders = locationManager.getAllProviders();
for (String provider: matchingProviders) {
  Location location = locationManager.getLastKnownLocation(provider);
  if (location != null) {
    float accuracy = location.getAccuracy();
    long time = location.getTime();

    if ((time > minTime && accuracy < bestAccuracy)) {
      bestResult = location;
      bestAccuracy = accuracy;
      bestTime = time;
    }
    else if (time < minTime && 
             bestAccuracy == Float.MAX_VALUE && time > bestTime){
      bestResult = location;
      bestTime = time;
    }
  }
}

如果上一个已知位置不够新,您可以使用最快的位置提供商请求单个位置更新:

if (locationListener != null &&
   (bestTime < maxTime || bestAccuracy > maxDistance)) { 
  IntentFilter locIntentFilter = new IntentFilter(SINGLE_LOCATION_UPDATE_ACTION);
  context.registerReceiver(singleUpdateReceiver, locIntentFilter);      
  locationManager.requestSingleUpdate(criteria, singleUpatePI);
}

显然,您需要配置 BroadcastReceiver

protected BroadcastReceiver singleUpdateReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    context.unregisterReceiver(singleUpdateReceiver);

    String key = LocationManager.KEY_LOCATION_CHANGED;
    Location location = (Location)intent.getExtras().get(key);

    if (locationListener != null && location != null)
      locationListener.onLocationChanged(location);

    locationManager.removeUpdates(singleUpatePI);
  }
};
相关问题