我想構建在用戶離線時提供當前位置的代碼。我創建了一個代碼,其中我使用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);
此鏈接(https://stackoverflow.com/queue stions/6694391/android-get-current-location-of-user-without-using-gps-or-internet)與您需要的不完全相同,但可能會有所幫助。 – procrastinator