Skip to content

Instantly share code, notes, and snippets.

@farooqkhan003
Last active January 15, 2017 14:05
Show Gist options
  • Select an option

  • Save farooqkhan003/cc973af94e6f786ac1b80452ac8a5906 to your computer and use it in GitHub Desktop.

Select an option

Save farooqkhan003/cc973af94e6f786ac1b80452ac8a5906 to your computer and use it in GitHub Desktop.
package com.demo.farooq.currentlocation;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.content.IntentSender.SendIntentException;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.location.LocationSettingsStates;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import static com.google.android.gms.common.api.GoogleApiClient.*;
public class MainActivity extends AppCompatActivity implements
ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
TextView _latitude, _longitude;
ProgressBar _progressBar;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
LocationRequest mLocationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_latitude = (TextView) findViewById(R.id.latitude);
_longitude = (TextView) findViewById(R.id.longitude);
_progressBar = (ProgressBar) findViewById(R.id.progressBar);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
} else
Toast.makeText(this, "Not Connected!", Toast.LENGTH_SHORT).show();
}
/*Ending the updates for the location service*/
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
settingRequest();
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(this, "Connection Suspended!", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(this, "Connection Failed!", Toast.LENGTH_SHORT).show();
if (connectionResult.hasResolution()) {
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(this, 90000);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
} else {
Log.i("Current Location", "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
/*Method to get the enable location settings dialog*/
public void settingRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000); // 10 seconds, in milliseconds
mLocationRequest.setFastestInterval(1000); // 1 second, in milliseconds
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient,
builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(@NonNull LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can
// initialize location requests here.
getLocation();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(MainActivity.this, 1000);
} catch (SendIntentException e) {
// Ignore the error.
}
break;
case 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.
break;
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
switch (requestCode) {
case 1000:
switch (resultCode) {
case Activity.RESULT_OK:
// All required changes were successfully made
getLocation();
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to
Toast.makeText(this, "Location Service not Enabled", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
break;
}
}
public void getLocation() {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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;
} else {
/*Getting the location after aquiring location service*/
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
_progressBar.setVisibility(View.INVISIBLE);
_latitude.setText("Latitude: " + String.valueOf(mLastLocation.getLatitude()));
_longitude.setText("Longitude: " + String.valueOf(mLastLocation.getLongitude()));
} else {
/*if there is no last known location. Which means the device has no data for the loction currently.
* So we will get the current location.
* For this we'll implement Location Listener and override onLocationChanged*/
Log.i("Current Location", "No data for location found");
if (!mGoogleApiClient.isConnected())
mGoogleApiClient.connect();
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, MainActivity.this);
}
}
}
/*When Location changes, this method get called. */
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
_progressBar.setVisibility(View.INVISIBLE);
_latitude.setText("Latitude: " + String.valueOf(mLastLocation.getLatitude()));
_longitude.setText("Longitude: " + String.valueOf(mLastLocation.getLongitude()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment