2017-10-09 108 views
0

我想構建在用戶離線時提供當前位置的代碼。我創建了一個代碼,其中我使用GPS提供商,但我只在用戶在線時給出當前位置。我想獲得的用戶 當前和新鮮的位置下面是代碼Android:如何使用GPS提供商離線當前位置?

final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
    LocationListener locationListener=new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 

      lat=location.getLatitude(); 
      String mm = ""; 
      longitude=location.getLongitude(); 
      locationManager.removeUpdates(this); 
      Geocoder geocoder = new Geocoder(context, Locale.getDefault()); 
      try { 
       List<Address> addresses = geocoder.getFromLocation(lat, longitude, 1); 
       Address obj = addresses.get(0); 
       mm = obj.getCountryName(); 
       mm=mm+" , "+obj.getSubAdminArea(); 
       mm=mm+" , "+obj.getSubLocality(); 
       mm=mm+" , "+obj.getFeatureName(); 



       Log.v("IGA", "Address" + mm); 


      } catch (IOException e) { 
       e.printStackTrace(); 
      } 






     } 

     @Override 
     public void onStatusChanged(String s, int i, Bundle bundle) { 

     } 

     @Override 
     public void onProviderEnabled(String s) { 

     } 

     @Override 
     public void onProviderDisabled(String s) { 

     } 
    }; 
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     String[] permissions, 
               int[] grantResults) 

     return; 
    } 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
+0

此鏈接(https://stackoverflow.com/queue stions/6694391/android-get-current-location-of-user-without-using-gps-or-internet)與您需要的不完全相同,但可能會有所幫助。 – procrastinator

回答

1

請在線定義/offlinË

我建議你使用:locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, locationListenerGPS, null);

更新的代碼應該是:

final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
LocationListener locationListener=new LocationListener() { 
    @Override 
    public void onLocationChanged(Location location) { 

     lat=location.getLatitude(); 
     String mm = ""; 
     longitude=location.getLongitude(); 

     Geocoder geocoder = new Geocoder(context, Locale.getDefault()); 
     try { 
      List<Address> addresses = geocoder.getFromLocation(lat, longitude, 1); 
      Address obj = addresses.get(0); 
      mm = obj.getCountryName(); 
      mm=mm+" , "+obj.getSubAdminArea(); 
      mm=mm+" , "+obj.getSubLocality(); 
      mm=mm+" , "+obj.getFeatureName(); 

      Log.v("IGA", "Address" + mm); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

    @Override 
    public void onStatusChanged(String s, int i, Bundle bundle) { 

    } 

    @Override 
    public void onProviderEnabled(String s) { 

    } 

    @Override 
    public void onProviderDisabled(String s) { 

    } 
}; 
if (ActivityCompat.checkSelfPermission(context, 
    Manifest.permission.ACCESS_FINE_LOCATION) != 
    PackageManager.PERMISSION_GRANTED 
    && 
    ActivityCompat.checkSelfPermission(context, 
    Manifest.permission.ACCESS_COARSE_LOCATION) 
    != PackageManager.PERMISSION_GRANTED) { 
    String[] permissions, int[] grantResults) 

    return; 
} 
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, locationListenerGPS, null); 
+0

我想使用沒有互聯網 –

+0

如果你只需要請求位置一次,使用我給的代碼,它沒有互聯網工作。 你只需要互聯網地理編碼器,檢索地址不是位置(經度和緯度) –

+0

非常感謝,先生其工作:) –

相關問題