Code example for LocationManager: Finding current location

Methods: addTestProvidergetLastKnownLocationrequestLocationUpdatessetTestProviderEnabledsetTestProviderLocation

1
	public void testPreconditions() { 
		assertNotNull(getActivity());
	} 
 
	public void testAddingLocationToATask() { 
		manager.addTestProvider(MOCK_GPS_PROVIDER, false, false, false, false,
				true, true, true, 0, 5); 
 
		// Setting a fake location to the mock provider 
		Location location = new Location(MOCK_GPS_PROVIDER);
		location.setLatitude(42.0);
		location.setLongitude(1337.0);
		location.setTime(System.currentTimeMillis()); // necessary to wake up
														// LocationListener 
		manager.setTestProviderLocation(MOCK_GPS_PROVIDER, location);
 
		// Enable mock provider 
		manager.setTestProviderEnabled(MOCK_GPS_PROVIDER, true);
 
		// Getting location updates 
		manager.requestLocationUpdates(MOCK_GPS_PROVIDER, 0, 0, getActivity());
 
		Location gpsLocation = manager.getLastKnownLocation(MOCK_GPS_PROVIDER);
 
		assertNotNull("No GPS available on the test device", gpsLocation);
 
		assertEquals(location.getLatitude(), gpsLocation.getLatitude());
		assertEquals(location.getLongitude(), gpsLocation.getLongitude());