Skip to content

Instantly share code, notes, and snippets.

@Bahaaib
Last active June 11, 2017 22:22
Show Gist options
  • Select an option

  • Save Bahaaib/6da44adfb6e233dd9edd625535fe0a17 to your computer and use it in GitHub Desktop.

Select an option

Save Bahaaib/6da44adfb6e233dd9edd625535fe0a17 to your computer and use it in GitHub Desktop.
How to get User location information via android system
package com.example.robpercival.locationdemo;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity implements LocationListener {
LocationManager locationManager; // creating an object of class Location manager
String provider;
@Override // An Override function that responsible for the code running in the background and connot affect the UI
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // calling the location service to the Location manager class
provider = locationManager.getBestProvider(new Criteria(), false); // get the location from the best provider based on a certain criteria like
// low power consuming, false to check up till meet the criteria - true to
//use the current criteria.
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
Log.i("Location Info", "Location achieved!");
} else {
Log.i("Location Info", "No location :(");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override // A method responsible for requesting the user current location once the app being resumed in use.
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this); // get location via provider, if min time, if min dist, for this context
}
@Override // A method that handles what should happen when the app is in the background
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this); // Updates should be removed due not to consume alot of battery power
}
@Override // A Generated method related to [implement LocationListener extended class] detects if user location changed
public void onLocationChanged(Location location) {
Double lat = location.getLatitude(); // get user latitude
Double lng = location.getLongitude(); // get user longtide
Log.i("Location info: Lat", lat.toString());
Log.i("Location info: Lng", lng.toString());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
public void getLocation(View view) { // method to get the last user known location if the provider service is not available
Location location = locationManager.getLastKnownLocation(provider); // get last known location
onLocationChanged(location); // use this value to return to the locationChanged method
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment