Получить текущее местоположение с помощью объединенного провайдера местоположения
У меня есть эта активность, и я хочу получить текущее местоположение, но пока не знаю, как правильно ее завершить. Кроме того, что такое REQUEST_CODE_ASK_PERMISSIONS?? я должен генерировать один?
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private double longitude;
private double latitude;
private TextView latitudeText, longitudeText;
private FusedLocationProviderClient fusedLocationProviderClient;
private GoogleApiClient googleApiClient;
private LocationManager locationManager;
private LocationListener locationListener;
private LocationRequest locationRequest;
private LocationCallback locationCallback;
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitudeText = findViewById(R.id.latitudeText);
longitudeText = findViewById(R.id.longitudeText);
getLocation();
}
private void getLocation() {
locationRequest = new LocationRequest()
.setInterval(2000).setFastestInterval(2000).setPriority(1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
latitude = location.getLatitude();
longitude = location.getLongitude();
latitudeText.setText(String.valueOf(latitude));
longitudeText.setText(String.valueOf(longitude));
}
}
};
task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
}
});
}
@Override
protected void onResume() {
super.onResume();
if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest
.permission.ACCESS_FINE_LOCATION},REQUEST_CODE_ASK_PERMISSIONS);
} else if(){
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
.....................................................................................................................................................
5 ответов
Добавить зависимость в build.gradle (Модуль: приложение)
implementation 'com.google.android.gms:play-services-location:16.0.0'
Объявить необходимое разрешение в манифесте
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
EasyLocationProvider.java
import android.annotation.SuppressLint;
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.OnLifecycleEvent;
import android.content.Context;
import android.content.IntentSender;
import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResolvableApiException;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResponse;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import com.google.android.gms.location.SettingsClient;
import com.google.android.gms.tasks.OnCanceledListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
public class EasyLocationProvider implements LifecycleObserver {
private EasyLocationCallback callback;
private Context context;
private FusedLocationProviderClient mFusedLocationClient;
private SettingsClient mSettingsClient;
private LocationCallback mLocationCallback;
private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
private LocationSettingsRequest mLocationSettingsRequest;
private long interval;
private long fastestInterval;
private int priority;
private double Latitude = 0.0, Longitude = 0.0;
private EasyLocationProvider(final Builder builder) {
context = builder.context;
callback = builder.callback;
interval = builder.interval;
fastestInterval = builder.fastestInterval;
priority = builder.priority;
}
@SuppressLint("MissingPermission")
public void requestLocationUpdate() {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
}
private void connectGoogleClient() {
GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
int resultCode = googleAPI.isGooglePlayServicesAvailable(context);
if (resultCode == ConnectionResult.SUCCESS) {
mGoogleApiClient.connect();
} else {
int REQUEST_GOOGLE_PLAY_SERVICE = 988;
googleAPI.getErrorDialog((AppCompatActivity) context, resultCode, REQUEST_GOOGLE_PLAY_SERVICE);
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
private void onCreateLocationProvider() {
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
private void onLocationResume() {
buildGoogleApiClient();
}
@SuppressLint("MissingPermission")
private synchronized void buildGoogleApiClient() {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
mSettingsClient = LocationServices.getSettingsClient(context);
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
callback.onGoogleAPIClient(mGoogleApiClient, "Connected");
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(interval);
mLocationRequest.setFastestInterval(fastestInterval);
mLocationRequest.setPriority(priority);
mLocationRequest.setSmallestDisplacement(0);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
builder.setAlwaysShow(true);
mLocationSettingsRequest = builder.build();
mSettingsClient
.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
showLog("GPS is Enabled Requested Location Update");
requestLocationUpdate();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
int statusCode = ((ApiException) e).getStatusCode();
switch (statusCode) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
int REQUEST_CHECK_SETTINGS = 214;
ResolvableApiException rae = (ResolvableApiException) e;
rae.startResolutionForResult((AppCompatActivity) context, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sie) {
showLog("Unable to Execute Request");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
showLog("Location Settings are Inadequate, and Cannot be fixed here. Fix in Settings");
}
}
}).addOnCanceledListener(new OnCanceledListener() {
@Override
public void onCanceled() {
showLog("onCanceled");
}
});
}
@Override
public void onConnectionSuspended(int i) {
connectGoogleClient();
callback.onGoogleAPIClient(mGoogleApiClient, "Connection Suspended");
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
callback.onGoogleAPIClient(mGoogleApiClient, "" + connectionResult.getErrorCode() + " " + connectionResult.getErrorMessage());
}
})
.addApi(LocationServices.API)
.build();
connectGoogleClient();
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(final LocationResult locationResult) {
super.onLocationResult(locationResult);
Latitude = locationResult.getLastLocation().getLatitude();
Longitude = locationResult.getLastLocation().getLongitude();
if (Latitude == 0.0 && Longitude == 0.0) {
showLog("New Location Requested");
requestLocationUpdate();
} else {
callback.onLocationUpdated(Latitude, Longitude);
}
}
};
}
@SuppressLint("MissingPermission")
public void removeUpdates() {
try {
callback.onLocationUpdateRemoved();
} catch (Exception e) {
e.printStackTrace();
}
}
private void showLog(String message) {
Log.e("EasyLocationProvider", "" + message);
}
public interface EasyLocationCallback {
void onGoogleAPIClient(GoogleApiClient googleApiClient, String message);
void onLocationUpdated(double latitude, double longitude);
void onLocationUpdateRemoved();
}
public static class Builder {
private Context context;
private EasyLocationCallback callback;
private long interval = 10 * 1000;
private long fastestInterval = 5 * 1000;
private int priority = LocationRequest.PRIORITY_HIGH_ACCURACY;
public Builder(Context context) {
this.context = context;
}
public EasyLocationProvider build() {
if (callback == null) {
Toast.makeText(context, "EasyLocationCallback listener can not be null", Toast.LENGTH_SHORT).show();
}
return new EasyLocationProvider(this);
}
public Builder setListener(EasyLocationCallback callback) {
this.callback = callback;
return this;
}
public Builder setInterval(long interval) {
this.interval = interval;
return this;
}
public Builder setFastestInterval(int fastestInterval) {
this.fastestInterval = fastestInterval;
return this;
}
public Builder setPriority(int priority) {
this.priority = priority;
return this;
}
}
}
Использование:
EasyLocationProvider easyLocationProvider; //Declare Global Variable
easyLocationProvider = new EasyLocationProvider.Builder(BottomNavigationActivity.this)
.setInterval(5000)
.setFastestInterval(2000)
//.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setListener(new EasyLocationProvider.EasyLocationCallback() {
@Override
public void onGoogleAPIClient(GoogleApiClient googleApiClient, String message) {
Log.e("EasyLocationProvider","onGoogleAPIClient: "+message);
}
@Override
public void onLocationUpdated(double latitude, double longitude) {
Log.e("EasyLocationProvider","onLocationUpdated:: "+ "Latitude: "+latitude+" Longitude: "+longitude);
}
@Override
public void onLocationUpdateRemoved() {
Log.e("EasyLocationProvider","onLocationUpdateRemoved");
}
}).build();
getLifecycle().addObserver(easyLocationProvider);
Удалить обратный вызов обновления местоположения
@Override
protected void onDestroy() {
easyLocationProvider.removeUpdates();
getLifecycle().removeObserver(easyLocationProvider);
super.onDestroy();
}
ПРИМЕЧАНИЕ. Предоставьте разрешения для устройств "Зефир" и "Над версией".
Ответ Кетана Рамани помог мне и всем кредитам ему. Я сделал небольшую корректировку, чтобы можно было устанавливать количество обновлений, особенно если требуется только одно.
public class LocationProvider implements LifecycleObserver {
private EasyLocationCallback callback;
private Context context;
private FusedLocationProviderClient mFusedLocationClient;
private SettingsClient mSettingsClient;
private LocationCallback mLocationCallback;
private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
private LocationSettingsRequest mLocationSettingsRequest;
private long interval;
private long fastestInterval;
private int priority;
private int numberOfUpdates;
private double Latitude = 0.0, Longitude = 0.0;
private LocationProvider(final Builder builder) {
context = builder.context;
callback = builder.callback;
interval = builder.interval;
fastestInterval = builder.fastestInterval;
priority = builder.priority;
numberOfUpdates = builder.numberOfUpdates;
}
@SuppressLint("MissingPermission")
public void requestLocationUpdate() {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
}
private void connectGoogleClient() {
GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
int resultCode = googleAPI.isGooglePlayServicesAvailable(context);
if (resultCode == ConnectionResult.SUCCESS) {
mGoogleApiClient.connect();
} else {
int REQUEST_GOOGLE_PLAY_SERVICE = 988;
googleAPI.getErrorDialog((AppCompatActivity) context, resultCode, REQUEST_GOOGLE_PLAY_SERVICE);
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
private void onCreateLocationProvider() {
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
private void onLocationResume() {
buildGoogleApiClient();
}
@SuppressLint("MissingPermission")
private synchronized void buildGoogleApiClient() {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
mSettingsClient = LocationServices.getSettingsClient(context);
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
callback.onGoogleAPIClient(mGoogleApiClient, "Connected");
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(interval);
mLocationRequest.setFastestInterval(fastestInterval);
mLocationRequest.setPriority(priority);
mLocationRequest.setSmallestDisplacement(0);
if (numberOfUpdates > 0) mLocationRequest.setNumUpdates(numberOfUpdates);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
builder.setAlwaysShow(true);
mLocationSettingsRequest = builder.build();
mSettingsClient
.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener(locationSettingsResponse -> {
showLog("GPS is Enabled Requested Location Update");
requestLocationUpdate();
}).addOnFailureListener(e -> {
int statusCode = ((ApiException) e).getStatusCode();
switch (statusCode) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
int REQUEST_CHECK_SETTINGS = 214;
ResolvableApiException rae = (ResolvableApiException) e;
rae.startResolutionForResult((AppCompatActivity) context, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sie) {
showLog("Unable to Execute Request");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
showLog("Location Settings are Inadequate, and Cannot be fixed here. Fix in Settings");
}
}).addOnCanceledListener(new OnCanceledListener() {
@Override
public void onCanceled() {
showLog("onCanceled");
}
});
}
@Override
public void onConnectionSuspended(int i) {
connectGoogleClient();
callback.onGoogleAPIClient(mGoogleApiClient, "Connection Suspended");
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
callback.onGoogleAPIClient(mGoogleApiClient, "" + connectionResult.getErrorCode() + " " + connectionResult.getErrorMessage());
}
})
.addApi(LocationServices.API)
.build();
connectGoogleClient();
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(final LocationResult locationResult) {
super.onLocationResult(locationResult);
Latitude = locationResult.getLastLocation().getLatitude();
Longitude = locationResult.getLastLocation().getLongitude();
if (Latitude == 0.0 && Longitude == 0.0) {
showLog("New Location Requested");
requestLocationUpdate();
} else {
callback.onLocationUpdated(Latitude, Longitude);
}
}
};
}
public LatLng getLastLocation() {
if ( ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_COARSE_LOCATION ) == PackageManager.PERMISSION_GRANTED ) {
Location location = mFusedLocationClient.getLastLocation().getResult();
return new LatLng(location.getLatitude(), location.getLongitude());
}
return null;
}
@SuppressLint("MissingPermission")
public void removeUpdates() {
try {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
callback.onLocationUpdateRemoved();
} catch (Exception e) {
e.printStackTrace();
}
}
private void showLog(String message) {
Log.e("LocationProvider", "" + message);
}
public interface EasyLocationCallback {
void onGoogleAPIClient(GoogleApiClient googleApiClient, String message);
void onLocationUpdated(double latitude, double longitude);
void onLocationUpdateRemoved();
}
public static class Builder {
private Context context;
private EasyLocationCallback callback;
private long interval = 10 * 1000;
private long fastestInterval = 5 * 1000;
private int priority = LocationRequest.PRIORITY_HIGH_ACCURACY;
private int numberOfUpdates = 0;
public Builder(Context context) {
this.context = context;
}
public LocationProvider build() {
if (callback == null) {
Toast.makeText(context, "EasyLocationCallback listener can not be null", Toast.LENGTH_SHORT).show();
}
return new LocationProvider(this);
}
public Builder setListener(EasyLocationCallback callback) {
this.callback = callback;
return this;
}
public Builder setInterval(long interval) {
this.interval = interval;
return this;
}
public Builder setFastestInterval(int fastestInterval) {
this.fastestInterval = fastestInterval;
return this;
}
public Builder setPriority(int priority) {
this.priority = priority;
return this;
}
public Builder setNumberOfUpdates(int numberOfUpdates) {
this.numberOfUpdates = numberOfUpdates;
return this;
}
}
}
Настройка местоположения Provider
locationProvider = new LocationProvider.Builder(MainActivity.this)
.setInterval(10000)
.setFastestInterval(5000)
.setNumberOfUpdates(1) // If you want infinite updates, remove this line entirely
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setListener(new LocationProvider.EasyLocationCallback() {
@Override
public void onGoogleAPIClient(GoogleApiClient googleApiClient, String message) {
Log.e("LocationProvider", "onGoogleAPIClient: " + message);
}
@Override
public void onLocationUpdated(double updateLatitude, double updateLongitude) {
// You could also add some intelligence to remove the location listener once counter == numberOfUpdates
}
@Override
public void onLocationUpdateRemoved() {
Log.e("LocationProvider", "onLocationUpdateRemoved");
}
}).build();
getLifecycle().addObserver(locationProvider);
Вы должны переопределить onRequestPermissionsResult
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLocation();
}
}
}
}
Так что вам решать REQUEST_CODE_ASK_PERMISSIONS
и его роль заключается в обработке обратного вызова, особенно если вы запрашиваете больше, чем на разрешение.
И я думаю, что ваш getLocation()
использует устаревший API. Посмотрите здесь, как получить местоположение.
Попробуйте описанный ниже способ получить местоположение с помощью FusedLocationProviderClient.
public class LocationTrackerClient{
public static String TAG = LocationTrackerClient.class.getName();
private Location mLastLocation;
LocationRequest mLocationRequest;
private FusedLocationProviderClient mFusedLocationClient;
static Context mcontext;
private static int UPDATE_INTERVAL = 1000;
private static int FATEST_INTERVAL = 1000;
private static int DISPLACEMENT = 5;
private static LocationTrackerClient instance;
private boolean isConnected = false;
public static synchronized LocationTrackerClient getInstance(Context ctx) {
mcontext = ctx;
if (instance == null) {
instance = new LocationTrackerClient();
}
return instance;
}
public void getLocationclient() {
try {
mFusedLocationClient = null;
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(mcontext);
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FATEST_INTERVAL);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
} catch (Exception e) {
ExceptionHandler.printStackTrace(e);
}
}
public void connectToLocation() {
stopLocationUpdates();
getLocationclient();
displayLocation();
}
public void stopLocationUpdates() {
removeFusedLocationUpdate();
}
public void removeFusedLocationUpdate() {
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(mcontext,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(mcontext, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
}else{
return;
}
}
try {
if (mFusedLocationClient != null) {
mFusedLocationClient
.removeLocationUpdates(mLocationCallback)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Logger.d(TAG, "Location updates stopped at removeFusedLocationUpdate! == ");
// Toast.makeText(getApplicationContext(), "Location updates stopped!", Toast.LENGTH_SHORT).show();
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void displayLocation() {
try {
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback, Looper.myLooper());
} catch (SecurityException e) {
ExceptionHandler.printStackTrace(e);
} catch (Exception e) {
ExceptionHandler.printStackTrace(e);
}
}
private LocationCallback mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
Logger.e(TAG, "locationResult ==== " + locationResult);
if (locationResult != null) {
mLastLocation = locationResult.getLastLocation();
if (mLastLocation != null) {
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
updateLattitudeLongitude(latitude, longitude);
}
}
}
};
public void updateLattitudeLongitude(double latitude, double longitude) {
Logger.i(TAG, "updated Lat == " + latitude + " updated long == " + longitude);
SharedPreferenceManager sharedPreferenceManager = SharedPreferenceManager.getInstance();
sharedPreferenceManager.updateUserDeviceLatLong(latitude, longitude);
}
}
На всякий случай, если кто-то найдет для этого решение. Теперь вы можете использовать FusedLocationClient.getCurrentLocation в версии библиотеки play-services-location
17.1.0