Requesting location updates using LocationManager
suggest changeAs always, you need to make sure you have the required permissions.
public class MainActivity extends AppCompatActivity implements LocationListener{ private LocationManager mLocationManager = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); } @Override protected void onResume() { super.onResume(); try { mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } catch(SecurityException e){ // The app doesn't have the correct permissions } } @Override protected void onPause() { try{ mLocationManager.removeUpdates(this); } catch (SecurityException e){ // The app doesn't have the correct permissions } super.onPause(); } @Override public void onLocationChanged(Location location) { // We received a location update! Log.i("onLocationChanged", location.toString()); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents