Android 如果gps处于打开状态,位置管理器将不工作。如果gps处于打开状态,则其工作正常

Android 如果gps处于打开状态,位置管理器将不工作。如果gps处于打开状态,则其工作正常,android,gps,android-location,Android,Gps,Android Location,在我的代码中,布局加载时获取gps坐标。这是我的示例代码。如果GPS关闭,它工作正常。如果我打开gps,它不会加载gps坐标。当用户打开gps时,我需要获取他的gps坐标。那么问题是什么呢。我需要换衣服的地方。对不起我的英语 dialog = new ProgressDialog(FXPage.this); dialog.show(); dialog.setMessage("Getting Coordinates"); locationManager = (LocationMan

在我的代码中,布局加载时获取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();
      }

  • GPS在屋顶下不工作
  • 您可以先从网络提供商处获取位置更新(如果可用)
  • 如果不适用,则仅从GPS请求并启动倒计时
  • 现在,在计时器过期后,您可以检查位置是否仍然为空,然后警告用户“无法获取位置更新”,并停止GPS位置更新请求
  • 为了避免上述所有问题,您可能只想使用FusedLocation Api,它比以前的LocationApi工作得更好。 检查FusedLocation api的链接

  • GPS在屋顶下不工作
  • 您可以先从网络提供商处获取位置更新(如果可用)
  • 如果不适用,则仅从GPS请求并启动倒计时
  • 现在,在计时器过期后,您可以检查位置是否仍然为空,然后警告用户“无法获取位置更新”,并停止GPS位置更新请求
  • 为了避免上述所有问题,您可能只想使用FusedLocation Api,它比以前的LocationApi工作得更好。
    检查FusedLocation api的链接。

    这部分代码似乎很奇怪:

     @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;
        }
      }
    }
    

    代码的这一部分似乎很奇怪:

     @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;
        }
      }
    }
    

    @本纳吉·博贾:这是链接中的顺序。请先自己尝试一下,或者可以搜索教程,如果没有帮助,我肯定会:)@benarjee bojja:链接中的顺序。请先自己尝试一下,或者可以搜索教程,如果没有帮助,我肯定会:)