【问题标题】:Location Manager not working if gps is on.Its working fine if gps is of如果 gps 打开,位置管理器不起作用。如果 gps 打开,它工作正常
【发布时间】:2016-05-13 22:23:42
【问题描述】:

在我的代码中,当布局加载时,它会获取 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

        }

【问题讨论】:

    标签: android gps android-location


    【解决方案1】:
    1. GPS 不能在屋顶下工作。
    2. 如果可用,您可以先从网络提供商处获取位置更新。
    3. 如果 N/A 仅向 GPS 请求并启动 CountDownTimer
    4. 现在计时器到期后,您可以检查位置是否仍然为空,然后提醒用户“无法获取位置更新”并停止 GPS 位置更新请求。

    为了避免上述所有问题,您可能只想使用比以前的 LocationApi 更好的 FusedLocation Api。 检查this FusedLocation api 的链接。

    【讨论】:

    • @benarjee bojja:它在链接中。请先自己尝试一下,或者可以搜索教程,如果没有帮助,我肯定会:)
    【解决方案2】:

    你这部分代码好像真的很奇怪:

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

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多