代码之家  ›  专栏  ›  技术社区  ›  UMAR-MOBITSOLUTIONS

如何在安卓系统中更新位置并禁用GPS?

  •  0
  • UMAR-MOBITSOLUTIONS  · 技术社区  · 15 年前

    我正在使用以下代码获取GPS位置

    它引起了两个问题

    1) 当我的位置改变时/我将手机从一个区域移动到另一个区域时,我不会得到更新的gps位置(纬度/经度)

     double MyDeviceLatitude,MyDeviceLongitude;
    
    public void GetGPSLocation()
    {
    
     LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    
                           LocationListener myLocationListener = new CurrentLocationListener(); 
    
                           locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30, 30, myLocationListener);
    
                       Location   currentLocation = locationManager.getLastKnownLocation("gps"); 
    
    if(currentLocation != null)
                           {
    
                           MyDeviceLatitude = currentLocation.getLatitude();
                           MyDeviceLongitude = currentLocation.getLongitude();
    }else
    {
    
     currentLocation = locationManager.getLastKnownLocation("network");
     MyDeviceLatitude = currentLocation.getLatitude();
     MyDeviceLongitude = currentLocation.getLongitude();
    
    
    
    }
    
    
    
    public class CurrentLocationListener implements LocationListener{
    
                public void onLocationChanged(Location argLocation) 
                {
                    if (argLocation != null) 
                    {
                        MyDeviceLatitude = argLocation.getLatitude();
                        MyDeviceLongitude = argLocation.getLongitude();
                    }
    
                }
                public void onProviderDisabled(String provider) {
                }
    
                public void onProviderEnabled(String provider) {
                }
    
                public void onStatusChanged(String provider, int status, Bundle arg2) {
                }
                }
    

    }

    1 回复  |  直到 15 年前
        1
  •  0
  •   Community Mohan Dere    9 年前

    嗯,一定要检查这个类似的帖子,它使用的广播接收器似乎可以解决你的问题 Android: How to get location information from intent bundle extras when using LocationManager.requestLocationUpdates()