Android: requestLocationUpdates - android

I have managed to get a fix on an android device's location (both with network provider and gps provider) using:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
but i would like calculate the phones location at the same moment once using the NETWORK_PROVIDER and then the GPS_PROVIDER so that i can compare each accuracy together.
Does anyone know how to pinpoint the device once with NETWORK_PROVIDER and then with GPS_PROVIDER?

Use 2 Location Listeners
public class MainActivity extends Activity {
private Location networkLocation = null;
private Location gpsLocation = null;
private class NetworkLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
// if you only want one location
// if (networkLocation == null)
networkLocation = location;
if (gpsLocation != null) {
// do something
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onProviderDisabled(String provider) {}
}
private class GpsLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
// if you only want one location
// if (gpsLocation == null)
gpsLocation = location;
if (networkLocation != null) {
// do something
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onProviderDisabled(String provider) {}
}
}

Related

Getting user location takes a very long time inside Google Map FragmentActivity

I am having issues getting a location in my map activity. Basically, it gets the location, but takes a VERY long time (a few minutes). I'm using a singleton that requests a single location update to retrieve my location. This works fine across multiple other instances in my app. However when I try it in my map activity, it takes a very long time. Here is my class I use to get a location:
public class SingleShotLocationProvider {
public interface LocationCallback {
void onNewLocationAvailable(GPSCoordinates location);
}
public static void requestSingleUpdate(final Context context, final LocationCallback callback) {
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//If network is available
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Log.i("SingleShot", "network available");
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
try {
locationManager.requestSingleUpdate(criteria, new LocationListener() {
#Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(new GPSCoordinates(location.getLongitude(), location.getLatitude()));
}
#Override public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override public void onProviderEnabled(String provider) {}
#Override public void onProviderDisabled(String provider) {}
}, null);
}
catch (SecurityException e){
Log.e("Location", "security exception");
}
}
//If GPS is enabled
else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Log.i("SingleShot", "GPS available");
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
try {
locationManager.requestSingleUpdate(criteria, new LocationListener() {
#Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(new GPSCoordinates(location.getLongitude(), location.getLatitude()));
}
#Override public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override public void onProviderEnabled(String provider) {}
#Override public void onProviderDisabled(String provider) {}
}, null);
}
catch (SecurityException e){
Log.e("SingleShot", "security exception");
}
}
else{
Toast.makeText(context, "No network or GPS available. Try again later.", Toast.LENGTH_LONG).show();
}
}
public static class GPSCoordinates {
public float longitude = -1;
public float latitude = -1;
public GPSCoordinates(double lon, double lat) {
longitude = (float) lon;
latitude = (float) lat;
}
}
}
This class works well in most areas in my app. It works in services and fragments. However when I use it in my map activity (FragmentActivity), the singleton takes a long time to use the callback I send it . Here is how I am requesting a new location from my map activity:
public class Traffic extends FragmentActivity
implements OnMapReadyCallback {
MapFragment mapFragment;
GoogleMap map;
//...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//...
}
#Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
SingleShotLocationProvider.requestSingleUpdate(getApplicationContext(), new SingleShotLocationProvider.LocationCallback() {
#Override
public void onNewLocationAvailable(SingleShotLocationProvider.GPSCoordinates location) {
Log.i(TAG, location.toString());
}
});
}
}
Any thoughts on why this would be taking so long?
Try changing the Criteria to COARSE or something which is less accurate but adequate.
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
https://developer.android.com/reference/android/location/Criteria.html

Check if user disable GPS in settings in android

There is any solution to check if user disable GPS in settings?
I open my app, open top toolbar of android system, disable GPS and close this toolbar. In this moment I want to app check if status of GPS was changed.
I use check if GPS is active in onResume(), but this solution works only when user enable GPS, when disable onResume() is not called.
Any ideas?
Edit:
This is may class:
public class PrivacyActivity extends BaseActivity implements GpsStatus.Listener, LocationListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_privacy);
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
}
#Override
public void onGpsStatusChanged(int event) {
switch (event) {
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
Toast.makeText(this, "ads", Toast.LENGTH_LONG).show();
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
Toast.makeText(this, "ads1", Toast.LENGTH_LONG).show();
break;
}
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(getApplicationContext(), "ads2", Toast.LENGTH_LONG).show();
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "ads2", Toast.LENGTH_LONG).show();
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "ads2", Toast.LENGTH_LONG).show();
}
}
and when I disable gps I didn't see toast.
You can use the LocationListener class
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {
Log.i("Example", "GPS is ON");
}
public void onProviderDisabled(String provider) {
Log.i("Example", "GPS is OFF");
}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
You can get more info in http://developer.android.com/guide/topics/location/strategies.html
Reference the locationListener first.
LocationListener locationListener;
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
LatLng myPlace = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(myPlace).title("me"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(myPlace));
mMap.animateCamera(CameraUpdateFactory.zoomTo(8.0f));
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
};
onProviderDisabled method will be fired if the user has disabled GPS from settings.
The intent inside the method will take the user to location settings directly.
Tested and works fine.

Disable location updates from GPS

UPDATED QUESTION - STILL NOT WORKING
I've changed the code.
Now, deactivateAlerts and activateAlerts are in the MainActivity.
Now, inside activateAlerts(), I'm instantiating a GPSListener for the GPS Provider:
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
gpsListener = new GPSListener(getApplicationContext());
} else if
Where the GPSListener is this:
public class GPSListener implements LocationListener {
private Context mCtx;
private Location cLoc;
private LocationManager mLocMan;
public GPSListener (Context ctx){
mCtx = ctx;
mLocMan = (LocationManager) mCtx.getSystemService(Context.LOCATION_SERVICE);
mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
public Location getLocation()
{
cLoc = mLocMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return cLoc;
}
public void stop()
{
mLocMan.removeUpdates(GPSListener.this);
}
#Override
public void onLocationChanged(Location location) {
Log.i("UPDATING", "updating location");
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
}
With the deactiveAlerts() function, I execute the instruction to remove the GPS updates:
if(gpsListener!=null){
gpsListener.stop();
}
But the GPS is not stopping yet =(
What is the problem now?
ORIGINAL QUESTION
The user can activate or deactivate location updates.
If the location updates are activated, it works ok, but when the location updates are deactivated, the GPS is still waiting for updates (and the icon appears on the action bar).
The code is that:
public class Alerts extends Fragment {
MyLocationListener myLocationListener = null;
...
public void activateAlerts() {
locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
myLocationListener = new MyLocationListener();
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
Utils.MINIMUM_TIME_BETWEEN_UPDATE,
Utils.MINIMUM_DISTANCECHANGE_FOR_UPDATE,
myLocationListener
);
} else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
Utils.MINIMUM_TIME_BETWEEN_UPDATE,
Utils.MINIMUM_DISTANCECHANGE_FOR_UPDATE,
myLocationListener
);
} else {
locationManager.requestLocationUpdates(
LocationManager.PASSIVE_PROVIDER,
Utils.MINIMUM_TIME_BETWEEN_UPDATE,
Utils.MINIMUM_DISTANCECHANGE_FOR_UPDATE,
myLocationListener
);
}
}
});
...
}
public void deactivateAlerts() {
...
locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
if(myLocationListener != null) {
locationManager.removeUpdates(myLocationListener);
myLocationListener = null;
}
...
}
public class MyLocationListener implements android.location.LocationListener {
public void onLocationChanged(Location location) {
Log.i("UPDATING", "updating location");
}
public void onStatusChanged(String s, int i, Bundle b) {
Log.v("LocationListener", "onStatusChanged");
}
public void onProviderDisabled(String s) {
Log.v("LocationListener", "onProviderDisabled");
}
public void onProviderEnabled(String s) {
Log.v("LocationListener", "onProviderEnabled");
}
}
What is the problem?
I've read this posts:
Post1
Post2
Post3
Post4

How do I get the current location of mobile using GPS Tracker?

I need to get my mobile current location using GPS programmatically.
How do I do that?
There is a complete explanation of how obtaining the current position in the training pages of the official android documentation.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsListener);
LocationListener gpsListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
//
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};

NETWORK_PROVIDER always returns 0.0 0.0

i have a problem with a network_provider.
Here is my LocationListener:
`private final class LocListener implements LocationListener{
#Override
public void onLocationChanged(Location location) {
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}else Log.i("locListener", "null");
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}`
Here's my listener for coordinates:
private void gpsListener(){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, this.locListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0, 0, this.locListener2);
}
I want retrieve location indoor too, it's the reason to use network_provider.
But when I'm indoor method onLocationChanged() doesn't calls.
All needed permissions I added to manifest file.
Oh, and I want to retrieve location from cell towers without using Wi-Fi or other connections
Thanks for replies

Categories

Resources