I made this code and it's working, but the location can only be retrieved if I open Google Maps before launching the app. I want to make my app detect my current location instead of the last known location. I found that there is a method called getCurrentLocation() that can be used to retrieve my location but I don't know what to put in the parameters. Please help me.
This is my code:
private void getLocation() {
//Check Permissions again
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(Reclamation.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(Reclamation.this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.INTERNET}, REQUEST_PERMISSION_LOCATION);
}
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location LocationGps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location LocationNetwork = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location LocationPassive = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (LocationGps != null) {
double lat = LocationGps.getLatitude();
double longi = LocationGps.getLongitude();
latitude = String.valueOf(lat);
longitude = String.valueOf(longi);
} else if (LocationNetwork != null) {
double lat = LocationNetwork.getLatitude();
double longi = LocationNetwork.getLongitude();
latitude = String.valueOf(lat);
longitude = String.valueOf(longi);
} else if (LocationPassive != null) {
double lat = LocationPassive.getLatitude();
double longi = LocationPassive.getLongitude();
latitude = String.valueOf(lat);
longitude = String.valueOf(longi);
Toast.makeText(Reclamation.this, "Passive is not null :D", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Reclamation.this, "Can't Get Your Location", Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(Reclamation.this, SetLocation.class);
intent.putExtra("latt", latitude);
intent.putExtra("lonn", longitude);
Toast.makeText(Reclamation.this, latitude + " " + longitude, Toast.LENGTH_SHORT).show();
startActivityForResult(intent, 3);
}
}
With
api 'com.google.android.gms:play-services-location:18.0.0'
U can use this methods
FusedLocationProviderClient fusedLocationClient;
FusedLocationProviderClient locationProviderClient = LocationServices.
getFusedLocationProviderClient(this);
//LOCATION AVAILABILITY
fusedLocationClient
.getLocationAvailability()
.addOnSuccessListener(new OnSuccessListener<LocationAvailability>() {
#Override
public void onSuccess(LocationAvailability locationAvailability) {
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
}
});
//LAST LOCATION
fusedLocationClient.getLastLocation()
.addOnSuccessListener(new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
}
});
//AND MORE...
fusedLocationClient
.requestLocationUpdates()
...
The GoogleApiClient has been depreceted and now I am having some trouble learning the latest way to get constant location updates. Also, can I get the location updates using LocationCallback and LocationRequetMethod??
I am glad that I finally found it. The code is short. I have used LocationRequest and LocationCallback method. Also, I have used the fusedLocationProviderClient for requestLocationUpdates.
public class MainActivity extends AppCompatActivity {
private FusedLocationProviderClient fusedLocationProviderClient;
TextView locationTextView;
LocationRequest locationRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationTextView = findViewById(R.id.location_text);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
//Not the best practices to get runtime permissions, but still here I ask permissions.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 2);
}
//Instantiating the Location request and setting the priority and the interval I need to update the location.
locationRequest = locationRequest.create();
locationRequest.setInterval(100);
locationRequest.setFastestInterval(50);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
//instantiating the LocationCallBack
LocationCallback locationCallback = new LocationCallback() {
#Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult != null) {
if (locationResult == null) {
return;
}
//Showing the latitude, longitude and accuracy on the home screen.
for (Location location : locationResult.getLocations()) {
locationTextView.setText(MessageFormat.format("Lat: {0} Long: {1} Accuracy: {2}", location.getLatitude(),
location.getLongitude(), location.getAccuracy()));
}
}
}
};
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());
}
}
In activity_main.xml
<TextView
android:id="#+id/location_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/andika"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Also, before all these, I have added this in my dependencies
implementation 'com.google.android.gms:play-services-location:17.0.0'
And this in my Manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
And this keeps on updating in intervals you choose
Hi so continious location update is something you should avoid as it drains battery. You can use locationlistner where you could listen to location change. Say you want to update the location and get the attitude and longitude on every 10 meter change.
Sample code to check last,long every 100 meter
class LocationService : Service(), LocationListener {
protected var locationManager: LocationManager? = null
var checkGPS = false
var checkNetwork = false
// boolean canGetLocation = false;
var loc: Location? = null
// double latitude;
// double longitude;
override fun onBind(intent: Intent): IBinder? {
return null
}
override fun onLocationChanged(location: Location) {
// Toast.makeText(getApplicationContext(), Double.toString(location.getLatitude()) + location.getLongitude(), Toast.LENGTH_LONG).show();
}
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
override fun onProviderEnabled(provider: String) {}
override fun onProviderDisabled(provider: String) {}
override fun onCreate() {
super.onCreate()
location
}
Toast.makeText(getApplicationContext(), Double.toString(latitude) + longitude + "from method", Toast.LENGTH_LONG).show();
private val location: Location?
private get() {
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) !=
PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
)
!= PackageManager.PERMISSION_GRANTED
) {
}
locationManager = applicationContext
.getSystemService(LOCATION_SERVICE) as LocationManager
checkGPS = locationManager!!
.isProviderEnabled(LocationManager.GPS_PROVIDER)
checkNetwork = locationManager!!
.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
locationManager!!.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES.toFloat(), this
)
if (locationManager != null) {
val fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationClient.lastLocation.addOnSuccessListener { location ->
if (location != null) {
Toast.makeText(
applicationContext,
java.lang.Double.toString(location.latitude) + location.longitude + "from method",
Toast.LENGTH_LONG
).show()
}
}
}
Toast.makeText(getApplicationContext(), Double.toString(latitude) + longitude + "from method", Toast.LENGTH_LONG).show();
return loc
}
companion object {
private const val MIN_DISTANCE_CHANGE_FOR_UPDATES: Long = 100
private const val MIN_TIME_BW_UPDATES: Long = 30
}
}
In my application I'm trying to get coordinate from fixed marker at center of google maps. When i scroll map, i want to get coordinate and set it on the text view.
Here is my Kotlin code:
class ActivityMapsDSPBng : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarkerClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener {
...
private val TAG = "ActivityMapsDSPBng"
private lateinit var mGoogleApiClient: GoogleApiClient
private var mLocationManager: LocationManager? = null
lateinit var mLocation: Location
private var mLocationRequest: LocationRequest? = null
private val listener: com.google.android.gms.location.LocationListener? = null
private val UPDATE_INTERVAL = (2 * 1000).toLong()
private val FASTEST_INTERVAL: Long = 2000
lateinit var locationManager: LocationManager
override fun onStart() {
...
}
override fun onStop() {
...
}
override fun onConnectionSuspended(p0: Int) {
...
}
override fun onConnectionFailed(connectionResult: ConnectionResult) {
Log.i(TAG, "Connection failed. Error: " + connectionResult.errorCode)
}
override fun onLocationChanged(location: Location) {
var msg = "Update Location: Latitude " + location.latitude.toString() + " Longitude " + location.longitude.toString()
tv_dspbg_lat.setText("" + location.latitude)
tv_dspbg_long.setText("" + location.longitude)
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
}
override fun onConnected(p0: Bundle?) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return
}
startLocationUpdates()
var fusedLocationProviderClient: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationProviderClient.lastLocation
.addOnSuccessListener(this, OnSuccessListener<Location> { location ->
if (location != null){
mLocation = location
tv_dspbg_lat.setText("" + mLocation.latitude)
tv_dspbg_long.setText("" + mLocation.longitude)
}
})
}
protected fun startLocationUpdates(){
// Create location request
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(UPDATE_INTERVAL)
.setFastestInterval(FASTEST_INTERVAL)
// Request location updates
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
return
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this)
}
override fun onCreate(savedInstanceState: Bundle?) {
...
val mapsDSPBNGfragment = supportFragmentManager
.findFragmentById(R.id.maps_dspbg) as SupportMapFragment
mapsDSPBNGfragment.getMapAsync(this)
fusedLPCBg = LocationServices.getFusedLocationProviderClient(this)
mGoogleApiClient = GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build()
mLocationManager = this.getSystemService(Context.LOCATION_SERVICE) as LocationManager
checkLocation()
}
private fun checkLocation(): Boolean{
...
}
private fun isLocationEnabled(): Boolean{
...
}
private fun showAlert(){
val dialog = AlertDialog.Builder(this)
dialog.setTitle("Enable Location")
.setMessage("Your location setting is set to 'Off'. \nPlease enable location to " + "use this app")
.setPositiveButton("Location Settings", DialogInterface.OnClickListener { paramDialogInterface, paramInt ->
val myIntent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivity(myIntent)
})
.setNegativeButton("Cancel", DialogInterface.OnClickListener { paramDialogInterface, paramInt -> })
dialog.show()
}
}
I build code from https://androidteachers.com/kotlin-for-android/get-location-in-android-with-kotlin/
When I scrool maps, coordinates do not change according to the fixed maker.
Check my Java code. The fixed marker you are talking about is like an overlay View from your layout code. But if it's right at the center of the map, you can use the center of the camera:
mGoogleMap.setOnCameraIdleListener(new OnCameraIdleListener() {
#Override
public void onCameraIdle() {
// Get the center coordinate of the map, if the overlay view is center too
CameraPosition cameraPosition = mGoogleMap.getCameraPosition();
LatLng currentCenter = cameraPosition.target;
// Or get any coordinate if overlay view is not at the centered
// int[] location = new int[2];
// mOverlayView.getLocationInWindow(location);
// Point viewPosition = new Point(location[0], location[1]);
// LatLng currentCenter = mGoogleMap.getProjection().fromScreenLocation(viewPosition);
}
}
To get the center location of google map
LatLng centerLatLang = mMap.getProjection().getVisibleRegion().latLngBounds.getCenter();
I cannot get location in service with interval time, when app is background. Service works and gets location but location is not updating, gets old location. I test via xiaomi, maybe problem with miui? I tried about 5-10 way but result is same. Get location old when I open app location get new and close the app, get location with old latitude and longitude
for example first way:
locationManager = getApplicationContext().getSystemService(LOCATION_SERVICE) as LocationManager
isGPSEnable = locationManager!!.isProviderEnabled(LocationManager.GPS_PROVIDER)
isNetworkEnable = locationManager!!.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
if (!isGPSEnable && !isNetworkEnable) {
} else {
Log.e(TAG,"${isGPSEnable} ")
if (isGPSEnable) {
location = null
locationManager!!.requestLocationUpdates(LocationManager.GPS_PROVIDER, LastKnownService.LOCATION_INTERVAL.toLong(), 0f, this)
if (locationManager != null) {
location = locationManager!!.getLastKnownLocation(LocationManager.GPS_PROVIDER)
if (App.lastKnownLocation == null){
if (location != null) {
MLog.d(TAG,"${location!!.latitude} ${location!!.longitude} == none")
App.lastKnownLocation = location
sendLocation(App.lastKnownLocation!!)
latitude = location!!.getLatitude()
longitude = location!!.getLongitude()
fn_update(location!!)
}
}else{
if (location != null) {
MLog.d(TAG,"${location!!.latitude} ${location!!.longitude} == ${location!!.distanceTo(App.lastKnownLocation)}")
App.lastKnownLocation = location
latitude = location!!.getLatitude()
longitude = location!!.getLongitude()
fn_update(location!!)
sendLocation(App.lastKnownLocation!!)
}
}
}
}else if (isNetworkEnable) {
location = null
locationManager!!.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LastKnownService.LOCATION_INTERVAL.toLong(), 0f, this)
if (locationManager != null) {
location = locationManager!!.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
if (App.lastKnownLocation == null){
if (location != null) {
MLog.d(TAG,"${location!!.latitude} ${location!!.longitude} == none")
App.lastKnownLocation = location
sendLocation(App.lastKnownLocation!!)
latitude = location!!.getLatitude()
longitude = location!!.getLongitude()
fn_update(location!!)
}
}else{
if (location != null) {
MLog.d(TAG,"${location!!.latitude} ${location!!.longitude} == ${location!!.distanceTo(App.lastKnownLocation)}")
App.lastKnownLocation = location
latitude = location!!.getLatitude()
longitude = location!!.getLongitude()
fn_update(location!!)
sendLocation(App.lastKnownLocation!!)
}
}
}
}
}
second way:
mLocationManager!!.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL.toLong(), LOCATION_DISTANCE,
mLocationListeners[1])
private var mLocationListeners = arrayOf(LocationListener(LocationManager.GPS_PROVIDER), LocationListener(LocationManager.NETWORK_PROVIDER))
private inner class LocationListener(provider: String) : android.location.LocationListener {
internal var mLastLocation: Location
init {
Log.e(TAG, "LocationListener $provider")
mLastLocation = Location(provider)
}
override fun onLocationChanged(location: Location) {
Log.e(TAG, "onLocationChanged: $location")
try{
val mBuilder = NotificationCompat.Builder(this#LocationService, "123")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("location sended")
.setContentText("onlocation changed")
.setStyle(NotificationCompat.BigTextStyle()
.bigText("onlocation changed"))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
if (notificationManager != null) notificationManager!!.notify(1,mBuilder.build())
val distance = location.distanceTo(mLastLocation)
if (distance > 10){
mLastLocation = location
sendLocation(mLastLocation)
}
}catch (e :java.lang.Exception){
Log.e(TAG, "send http lat lon exception: $e")
}
mLastLocation.set(location)
}
override fun onProviderDisabled(provider: String) {
Log.e(TAG, "onProviderDisabled: $provider")
}
override fun onProviderEnabled(provider: String) {
Log.e(TAG, "onProviderEnabled: $provider")
}
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {
Log.e(TAG, "onStatusChanged: $provider")
}
}
You have to remove battery optimize restriction for your app in Battery optimization manually( go to battery option from Security app and select no restriction for your app), then it will work.. But how to do it programmatically , i am also searching for that..thanks
call this method:
private fun openMiuiPowerKeeper(context: Context) {
context.startActivity(Intent().apply {
setClassName(
"com.miui.powerkeeper",
"com.miui.powerkeeper.ui.HiddenAppsConfigActivity"
)
putExtra("package_name", context.packageName)
putExtra("package_label", context.getString(R.string.app_name))
flags = Intent.FLAG_ACTIVITY_NEW_TASK
})
}
it open window, where user need line "not restriction". And you need check is miui firmware on device or something else, for instance, like this:
private fun isFirmwareMiui(context: Context): Boolean {
if (Build.MANUFACTURER.equals("xiaomi", true)) {
context.packageManager.getInstalledPackages(PackageManager.GET_META_DATA).forEach {
return it.packageName.startsWith("com.miui.")
}
}
return false
}
I have an android application that need device current location (latitude and longitude). I've tried some tutorial on the net and specially some solutions from stack overflow, but they doesn't work well for me. My requirement is so simple: First I need it to be fast and need the location once when I the fragment starts. Second I need it to be as precise as possible, I mean it should use GPS first if GPS is not available then use Network provider.
For example, I've tried this solution but it return null after 30 second, but I know there is some all the things is ok because google map and other application works well !!!
Something that almost all the answers suggest is to use getLastKnownLocation(), but I suppose it's not the current and I don't want it if it is so.
can anyone suggest me some kind of simple and fast way to get the location just ONCE ?!
Here, you can use this...
Example usage:
public void foo(Context context) {
// when you need location
// if inside activity context = this;
SingleShotLocationProvider.requestSingleUpdate(context,
new SingleShotLocationProvider.LocationCallback() {
#Override public void onNewLocationAvailable(GPSCoordinates location) {
Log.d("Location", "my location is " + location.toString());
}
});
}
You might want to verify the lat/long are actual values and not 0 or something. If I remember correctly this shouldn't throw an NPE but you might want to verify that.
public class SingleShotLocationProvider {
public static interface LocationCallback {
public void onNewLocationAvailable(GPSCoordinates location);
}
// calls back to calling thread, note this is for low grain: if you want higher precision, swap the
// contents of the else and if. Also be sure to check gps permission/settings are allowed.
// call usually takes <10ms
public static void requestSingleUpdate(final Context context, final LocationCallback callback) {
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isNetworkEnabled) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
locationManager.requestSingleUpdate(criteria, new LocationListener() {
#Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(new GPSCoordinates(location.getLatitude(), location.getLongitude()));
}
#Override public void onStatusChanged(String provider, int status, Bundle extras) { }
#Override public void onProviderEnabled(String provider) { }
#Override public void onProviderDisabled(String provider) { }
}, null);
} else {
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
locationManager.requestSingleUpdate(criteria, new LocationListener() {
#Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(new GPSCoordinates(location.getLatitude(), location.getLongitude()));
}
#Override public void onStatusChanged(String provider, int status, Bundle extras) { }
#Override public void onProviderEnabled(String provider) { }
#Override public void onProviderDisabled(String provider) { }
}, null);
}
}
}
// consider returning Location instead of this dummy wrapper class
public static class GPSCoordinates {
public float longitude = -1;
public float latitude = -1;
public GPSCoordinates(float theLatitude, float theLongitude) {
longitude = theLongitude;
latitude = theLatitude;
}
public GPSCoordinates(double theLatitude, double theLongitude) {
longitude = (float) theLongitude;
latitude = (float) theLatitude;
}
}
}
For anyone interested in retrieving a single location update, in the best, idiomatic way, using the latest APIs, and the magic of Kotlin, here you go:
Gradle dependency:
dependencies {
...
implementation "com.google.android.gms:play-services-location:18.0.0"
...
}
Manifest permissions:
<manifest>
...
<!-- required only for LocationRequest.PRIORITY_HIGH_ACCURACY -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- required for all other priorities -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
...
</manifest>
Somewhere in your Extensions file:
// To use PRIORITY_HIGH_ACCURACY, you must have ACCESS_FINE_LOCATION permission.
// Any other priority will require just ACCESS_COARSE_LOCATION,
// but will not guarantee a location update
#SuppressLint("MissingPermission")
suspend fun FusedLocationProviderClient.awaitCurrentLocation(priority: Int): Location? {
return suspendCancellableCoroutine {
// to use for request cancellation upon coroutine cancellation
val cts = CancellationTokenSource()
getCurrentLocation(priority, cts.token)
.addOnSuccessListener {location ->
// remember location is nullable, this happens sometimes
// when the request expires before an update is acquired
it.resume(location)
}.addOnFailureListener {e ->
it.resumeWithException(e)
}
it.invokeOnCancellation {
cts.cancel()
}
}
}
In your fragment:
// need to register this anywhere before onCreateView, idealy as a field
private val permissionRequester = registerForActivityResult(
// you can use RequestPermission() contract if you only need 1 permission
ActivityResultContracts.RequestMultiplePermissions()
) { map ->
// If you requested 1 permission, change `map` to `isGranted`
// Keys are permissions Strings, values are isGranted Booleans
// An easy way to check if "any" permission was granted is map.containsValue(true)
// You can use your own logic for multiple permissions,
// but they have to follow the same checks here:
val response = map.entries.first()
val permission = response.key
val isGranted = response.value
when {
isGranted -> onPermissionGranted()
ActivityCompat.shouldShowRequestPermissionRationale(requireContext(), permission) -> {
// permission denied but not permanently, tell user why you need it.
// Idealy provide a button to request it again and another to dismiss
AlertDialog.Builder(requireContext())
.setTitle(R.string.perm_request_rationale_title)
.setMessage(R.string.perm_request_rationale)
.setPositiveButton(R.string.request_perm_again) { _, _ ->
requirePermission()
}
.setNegativeButton(R.string.dismiss, null)
.create()
.show()
}
else -> {
// permission permanently denied
// 1) tell user the app won't work as expected, or
// 2) take him to your app's info screen to manually change permissions, or
// 3) silently and gracefully degrade user experience
// I'll leave the implementation to you
}
}
}
onPermissionGranted function:
private fun onPermissionGranted() {
val lm = requireContext().getSystemService(Context.LOCATION_SERVICE) as LocationManager
if(LocationManagerCompat.isLocationEnabled(lm)) {
// you can do this your own way, eg. from a viewModel
// but here is where you wanna start the coroutine.
// Choose your priority based on the permission you required
val priority = LocationRequest.PRIORITY_HIGH_ACCURACY
lifecycleScope.launch {
val location = LocationServices
.getFusedLocationProviderClient(requireContext())
.awaitCurrentLocation(priority)
// do whatever with this location, notice that it's nullable
}
} else {
// prompt user to enable location or launch location settings check
}
}
Now all you have to do is add this to MyLocation button click listener:
private fun requirePermission() {
val permissions = arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
// optional: Manifest.permission.ACCESS_COARSE_LOCATION
)
permissionRequester.launch(permissions)
}
Note that this has the beauty of checking if the permission was already given implicitly, and not show a dialog/request if that was the case.
Ergo, always start your flow by launching the requester and only do your checks in its callback.
AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
Requesting User Permissions
build.gradle (Module: app)
dependencies {
...
implementation 'com.google.android.gms:play-services-location:15.0.0'
...
}
If you receive an error, check that your top-level build.gradle
contains a reference to the google() repo or to maven { url
"https://maven.google.com" }
Set Up Google Play Services
LocationService.kt
import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
import android.net.Uri
import android.os.Looper
import android.provider.Settings
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.common.api.ResolvableApiException
import com.google.android.gms.location.*
import org.jetbrains.anko.alert
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.okButton
object LocationService {
#SuppressLint("StaticFieldLeak")
private lateinit var fusedLocationProviderClient: FusedLocationProviderClient
private lateinit var locationRequest: LocationRequest
private val locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
doAsync {
location = locationResult.lastLocation
onSuccess(location)
}
}
}
private lateinit var onSuccess: (location : Location) -> Unit
private lateinit var onError: () -> Unit
lateinit var location: Location
fun init(activity: Activity) {
fusedLocationProviderClient = FusedLocationProviderClient(activity)
locationRequest = LocationRequest().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setInterval(1000).setFastestInterval(1000).setNumUpdates(1)
}
private fun checkLocationStatusAndGetLocation(activity: Activity) {
doAsync {
when {
ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED -> LocationServices.getSettingsClient(activity).checkLocationSettings(LocationSettingsRequest.Builder().addLocationRequest(locationRequest).setAlwaysShow(true).build()).addOnCompleteListener { task ->
doAsync {
try {
task.getResult(ApiException::class.java)
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper())
} catch (exception: ApiException) {
when (exception.statusCode) {
LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> {
try {
(exception as ResolvableApiException).startResolutionForResult(activity, 7025)
} catch (ex: Exception) {
promptShowLocation(activity)
}
}
LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
promptShowLocation(activity)
}
}
}
}
}
ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_FINE_LOCATION) -> activity.runOnUiThread {
activity.alert("To continue, allow the device to use location, witch uses Google's Location Service") {
okButton {
val ite = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", activity.packageName, null))
ite.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
activity.startActivity(ite)
onError()
}
negativeButton("Cancelar", { onError() })
onCancelled { onError() }
}.show()
}
else -> ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 7024)
}
}
}
private fun promptShowLocation(activity: Activity) {
activity.runOnUiThread {
activity.alert("To continue, allow the device to use location, witch uses Google's Location Service") {
okButton {
activity.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
onError()
}
negativeButton("Cancelar", { onError() })
onCancelled { onError() }
}.show()
}
}
fun onRequestPermissionsResult(activity: Activity, requestCode: Int, grantResults: IntArray) {
if (requestCode == 7024) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
checkLocationStatusAndGetLocation(activity)
} else {
onError()
}
}
}
fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int) {
if (requestCode == 7025) {
if (resultCode == Activity.RESULT_OK) {
checkLocationStatusAndGetLocation(activity)
} else {
onError()
}
}
}
fun getLocation(activity: Activity, onSuccess: () -> Unit, onError: () -> Unit) {
this.onSuccess = onSuccess
this.onError = onError
checkLocationStatusAndGetLocation(activity)
}
}
your activity
override fun onCreate(savedInstanceState: Bundle?) {
...
LocationService.init(this)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
LocationService.onRequestPermissionsResult(this, requestCode, grantResults)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
LocationService.onActivityResult(this, requestCode, resultCode)
}
private fun yourFunction() {
LocationService.getLocation(this, { location ->
//TODO: use the location
}, {
//TODO: display error message
})
}
AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
MainActivity.java:
public class MainActivity extends AppCompatActivity implements LocationListener {
private LocationManager locationManager;
private Location onlyOneLocation;
private final int REQUEST_FINE_LOCATION = 1234;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_FINE_LOCATION);
}
#Override public void onLocationChanged(Location location) {
onlyOneLocation = location;
locationManager.removeUpdates(this);
}
#Override public void onStatusChanged(String provider, int status, Bundle extras) { }
#Override public void onProviderEnabled(String provider) { }
#Override public void onProviderDisabled(String provider) { }
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_FINE_LOCATION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d("gps", "Location permission granted");
try {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates("gps", 0, 0, this);
}
catch (SecurityException ex) {
Log.d("gps", "Location permission did not work!");
}
}
break;
}
}
What you want to do is achieved using the LocationManager#requestSingleUpdate. This method attaches a listener in a given looper (if you want or have it) and notifies the location ASAP it is received, only once. The method you suggest is used only as an inexact position before the real one is given to you.
In any case it will be fast than milliseconds (unless you are lucky enough to start listening when a location came to the device). Think on the GPS as an element that you enable when waiting for locations and disable when you remove this listening. This behavior is done to avoid draining the user's battery.
So, to sum up:
The time between you start listening and you receive the position depends on the device's GPS (manufacture, location of the user, satellite coverage...)
There is a method in the Android SDK to listen for a single update.
By providing the a criteria object, you can manage which criterias are acceptable for you to receive a location. Stronger criterias means more time to get an accurate response.
// Get LocationManager object
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
//latitude of location
double myLatitude = myLocation.getLatitude();
//longitude og location
double myLongitude = myLocation.getLongitude();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
All the above answers are not worked for me so I answered this
Initially add the dependencies
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
after add the class MyLocationListiner.java
package com.example.firebase_auth;
/**
* Created by Chromicle(Ajay Prabhakar).
*/
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import static android.content.Context.LOCATION_SERVICE;
public class MyLocationListener implements LocationListener {
public static double latitude;
Context ctx;
Location location;
LocationManager locationManager;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
public static double longitude;
MyLocationListener(Context ctx) {
this.ctx = ctx;
try {
locationManager = (LocationManager) ctx.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Toast.makeText(ctx, "GPS Enable " + isGPSEnabled, Toast.LENGTH_LONG).show();
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Toast.makeText(ctx, "Network Enable " + isNetworkEnabled, Toast.LENGTH_LONG).show();
if ( Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission
( ctx, android.Manifest.permission.ACCESS_FINE_LOCATION )
!= PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission( ctx,
android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) { }
if (isGPSEnabled == true) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, this);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if (isNetworkEnabled==true) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, this);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
latitude = location.getLatitude();
longitude = location.getLongitude();
// Toast.makeText(ctx,"latitude: "+latitude+" longitude: "+longitude,Toast.LENGTH_LONG).show();
}
catch(Exception ex)
{
Toast.makeText(ctx,"Exception "+ex, Toast.LENGTH_LONG).show();
}
}
#Nullable
#Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
latitude=loc.getLatitude();
longitude=loc.getLongitude();
}
#Override
public void onProviderDisabled(String provider)
{
//print "Currently GPS is Disabled";
}
#Override
public void onProviderEnabled(String provider)
{
//print "GPS got Enabled";
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
To use that class add this method
location is stored in the Address string
public void getLocation(){
Double latitude = 0.0, longitude;
String message = "";
LocationManager mlocManager = null;
LocationListener mlocListener;
mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener(this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
latitude = MyLocationListener.latitude;
longitude = MyLocationListener.longitude;
message = message +"https://www.google.com/maps/dir/#"+ latitude +","+ longitude;
address=message;
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
if (latitude == 0.0) {
Toast.makeText(getApplicationContext(), "Currently gps has not found your location....", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "GPS is currently off...", Toast.LENGTH_LONG).show();
}
}
Hope it helpfull
I have created some classes using those you can easily get the current location. I used FusedLocationProviderClient for getting the current location.
First Add this into your manifest File:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Then Check for location permission:
private fun startCheckingLocation() {
if (checkLocationPermissions() == true) {
checkGPSEnabled()
} else {
askLocationPermission()
}
}
checkLocationPermissions method:
private fun checkLocationPermissions(): Boolean? {
return PermissionUtils.hasPermissions(
requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
}
checkGPSEnabled method:
private fun checkGPSEnabled() {
GpsUtils(requireContext()) {
it?.let {
startCheckingCurrentLocation()
}
}.apply {
turnGPSOn(gpsDialogCallback)
}
}
As OnActivityResult is Deprecated:
private val gpsDialogCallback = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { activityResult ->
activityResult?.let { result ->
when (result.resultCode) {
RESULT_OK -> {
startCheckingCurrentLocation()
}
RESULT_CANCELED -> {
}
}
}
}
startCheckingCurrentLocation method :
private fun startCheckingCurrentLocation() {
LocationUtils(requireContext()) { location ->
Log.d(TAG, ">>>>>>>>>>>>>>" + location.latitude + " " + location.longitude)
startIntentService(location)
}.apply {
startLocationUpdates()
}
}
For GPS I have created one class that you can simply put and use it:
GPSUtils:
class GpsUtils(
private val context: Context,
private val gpsStatus: (isEnable: Boolean?) -> Unit
) {
private val mSettingsClient: SettingsClient = LocationServices.getSettingsClient(context)
private val mLocationSettingsRequest: LocationSettingsRequest
private val locationManager: LocationManager =
context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
private val locationRequest: LocationRequest = LocationRequest.create()
init {
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.interval = 10 * 1000.toLong()
locationRequest.fastestInterval = 2 * 1000.toLong()
val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
mLocationSettingsRequest = builder.build()
builder.setAlwaysShow(true) //this is the key ingredient
}
// method for turn on GPS
fun turnGPSOn(gpsDialogCallback: ActivityResultLauncher<IntentSenderRequest>) {
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
gpsStatus.invoke(true)
} else {
mSettingsClient
.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener(
(context as Activity)
) {
// GPS is already enable, callback GPS status through listener
gpsStatus.invoke(true)
}
.addOnFailureListener(context) { e ->
when ((e as ApiException).statusCode) {
LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> try {
// Show the dialog by calling startResolutionForResult() and check the result in onActivityResult().
if (e is ResolvableApiException) {
try {
val intentSenderRequest = IntentSenderRequest.Builder(e.resolution).build()
gpsDialogCallback.launch(intentSenderRequest)
} catch (throwable: Throwable) {
// Ignore the error.
}
}
} catch (sie: IntentSender.SendIntentException) {
// Ignore the error.
Timber.i("PendingIntent unable to execute request.")
}
LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
val errorMessage =
"Location settings are inadequate, and cannot be fixed here. Fix in Settings."
Timber.e(errorMessage)
}
LocationSettingsStatusCodes.CANCELED -> {
val errorMessage =
"Location settings are inadequate, and cannot be fixed here. Fix in Settings."
Timber.e(errorMessage)
}
LocationSettingsStatusCodes.SUCCESS -> {
// All location settings are satisfied. The client can initialize location
// requests here.
val errorMessage =
"Location settings are inadequate, and cannot be fixed here. Fix in Settings."
Timber.e(errorMessage)
}
}
}
}
}
}
For checking location I have created one more class :
class LocationUtils(
context: Context,
private val latLng: (location: Location) -> Unit) {
private var fusedLocationClient: FusedLocationProviderClient? = null
private val locationRequest = LocationRequest.create()?.apply {
interval = 20 * 1000.toLong()
fastestInterval = 2 * 1000.toLong()
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
init {
fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
}
/**
* call when location permission is allowed and you want to fetch the last location of the user
*/
fun getLastLocation() {
fusedLocationClient?.lastLocation?.addOnSuccessListener { location ->
location?.let {
latLng.invoke(location)
stopLocationUpdates()
}
}
}
/**
* Requested location callback
*/
private val locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
locationResult ?: return
for (location in locationResult.locations) {
location?.let {
latLng.invoke(it)
stopLocationUpdates()
}
}
super.onLocationResult(locationResult)
}
}
/**
* call when location permission is already given to user and you want to receive continues location updates
*/
fun startLocationUpdates() {
fusedLocationClient?.requestLocationUpdates(
locationRequest,
locationCallback,
Looper.getMainLooper()
)
}
/**
* call when you want to stop location updates
*/
fun stopLocationUpdates() {
fusedLocationClient?.removeLocationUpdates(locationCallback)?.addOnCompleteListener { }
}
}
I have created one worker class to get the address from latlng:
class FetchAddressWorker(
context: Context,
workerParameters: WorkerParameters
) :
Worker(context, workerParameters) {
companion object {
const val TAG = "FetchAddressWorker"
}
override fun doWork(): Result {
val geoCoder = Geocoder(applicationContext, Locale.getDefault())
var errorMessage = ""
val lat: Double = inputData.getDouble(CPKConstants.LATITUDE, 0.0)
val lng: Double = inputData.getDouble(CPKConstants.LONGITUDE, 0.0)
var addresses: List<Address> = emptyList()
try {
addresses =
geoCoder.getFromLocation(lat, lng, 1)
} catch (ioException: IOException) {
// Catch network or other I/O problems.
errorMessage = "Service not available"
LogUtils.e(TAG, errorMessage, ioException)
} catch (illegalArgumentException: IllegalArgumentException) {
// Catch invalid latitude or longitude values.
errorMessage = "Invalid lat lng used"
LogUtils.e(
TAG,
"$errorMessage. Latitude = $lat , Longitude = $lng",
illegalArgumentException
)
}
// Handle case where no address was found.
if (addresses.isEmpty()) {
if (errorMessage.isEmpty()) {
errorMessage = "No Address Found"
LogUtils.e(TAG, errorMessage)
}
val data = Data.Builder()
.putString(
CPKConstants.FAILURE_RESULT,
errorMessage
)
.build()
return Result.failure(data)
} else {
val address: Address = addresses[0]
// Fetch the address lines using getAddressLine,
// join them, and send them to the thread.
val addressFragments = with(address) {
(0..maxAddressLineIndex).map { getAddressLine(it) }
}
LogUtils.i(TAG, "Address Found " + addressFragments.joinToString(separator = "\n"))
val data = Data.Builder()
.putString(
CPKConstants.SUCCESS_RESULT,
addressFragments.joinToString(separator = "\n")
)
.build()
// Indicate whether the work finished successfully with the Result
return Result.success(data)
}
}
}
Than use AddressResultReceiver in your fragment or activity:
internal inner class AddressResultReceiver(handler: Handler) : ResultReceiver(handler) {
override fun onReceiveResult(resultCode: Int, resultData: Bundle?) {
// Display the address string
// or an error message sent from the intent service.
val addressOutput = resultData?.getString(AppConstants.RESULT_DATA_KEY).orEmpty()
//displayAddressOutput()
// Show a toast message if an address was found.
if (resultCode == AppConstants.SUCCESS_RESULT) {
Boast.showText(requireContext(), "Address found = $addressOutput")
txtContinueWith.text = addressOutput
}
}
}
You will need to initialize this in fragment or activity where you will use the above receiver to get the address:
private var resultReceiver = AddressResultReceiver(Handler())
These are some constants that you should use as it is.
//Location Constants
const val LOCATION_SERVICE = "LOCATION_SERVICE"
const val SUCCESS_RESULT = 0
const val FAILURE_RESULT = 1
const val PACKAGE_NAME = "com.google.android.gms.location.sample.locationaddress"
const val RECEIVER = "$PACKAGE_NAME.RECEIVER"
const val RESULT_DATA_KEY = "${PACKAGE_NAME}.RESULT_DATA_KEY"
const val LOCATION_DATA_EXTRA = "${PACKAGE_NAME}.LOCATION_DATA_EXTRA"
Start worker from view
private fun startAddressWorkManager(location: Location) {
val inputData = Data.Builder()
.putDouble(CPKConstants.LATITUDE, location.latitude)
.putDouble(CPKConstants.LONGITUDE, location.longitude)
.build()
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val fetchAddressWorkRequest: WorkRequest =
OneTimeWorkRequestBuilder<FetchAddressWorker>()
.setConstraints(constraints)
.setInputData(inputData)
.build()
WorkManager
.getInstance(this)
.enqueue(fetchAddressWorkRequest)
WorkManager.getInstance(this).getWorkInfoByIdLiveData(fetchAddressWorkRequest.id)
.observe(this,
{ workInfo ->
when (workInfo.state) {
WorkInfo.State.SUCCEEDED -> {
if (workInfo.state.isFinished) {
val addressData =
workInfo.outputData.getString(CPKConstants.SUCCESS_RESULT)
Timber.d("AddressData %s", addressData)
_binding?.searchEdt?.setText(addressData)
}
}
else -> {
Timber.d("workInfo %s", workInfo)
}
}
})
}