LocationManager doesn't update a location - android

There is the following code for getting current location:
final LocationManager manager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener listener=new LocationListener() {
#Override
public void onLocationChanged(Location location) {
manager.removeUpdates(this);
Toast.makeText(MainActivity.this, "2", Toast.LENGTH_LONG).show();
if (location!=null) {
doSomeAction();
}
else {
Toast.makeText(MainActivity.this, LOCATION_IS_NULL_MESSAGE, Toast.LENGTH_LONG).show();
}
}
#Override
public void onProviderDisabled(String provider) {
manager.removeUpdates(this);
String newProvider=provider.equals(LocationManager.GPS_PROVIDER) ? LocationManager.NETWORK_PROVIDER : null;
if (newProvider==null) {
Toast.makeText(MainActivity.this, LOCATION_PROVIDERS_ARE_DISABLED_MESSAGE, Toast.LENGTH_LONG).show();
}
else {
manager.requestLocationUpdates(newProvider, 0, 0, this);
}
}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onStatusChanged(String provider, int status,
Bundle extras) {}
};
String provider=manager.isProviderEnabled(LocationManager.GPS_PROVIDER) ? LocationManager.GPS_PROVIDER :
LocationManager.NETWORK_PROVIDER;
Location location=manager.getLastKnownLocation(provider);
if (location!=null) {
doSomeAction();
}
else {
Toast.makeText(this, "1", Toast.LENGTH_LONG).show();
manager.requestLocationUpdates(provider, 0, 0, listener);
}
I see toast "1" each time, but I've never seen "2" toast, therefore my location isn't be updated. Please, tell me, I need to get my current location - how can I fix my problem? I use network location provider.

Did you actually attach the listener? With out attaching the listener its code is useless, but furthermore afaik: If the LocationManager has no Listener attached it will not update any location.

Try like this :
public class Locato extends Activity implements LocationListener {
LocationManager locman;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.idlayout);
locman = (LocatonManager) getSystemService(Context.LOCATION_SERVICE);
}
public void onStatusChanged(String pr, int s, Bundle a) {}
#Override
public void onProviderDisabled(String provider) {
Log.d("MyApp", "Provider disabled : " + provider);
}
#Override
public void onProviderEnabled(String provider) {
Log.d("MyApp", "Provider enabled : " + provider);
}
#Override
public void onLocationChanged(Location location) {
// do something
}
You must also add permissions to manifest : access coarse location and access fine location

Related

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.

requestSingleUpdate listener does not work

onLocationChanged event is not fired with requestSingleUpdate.
It seems that it depends on the looper attached ex:
locManager.requestSingleUpdate("network",locListener,Looper.getMainLooper());
locManager.requestSingleUpdate("network",locListener,null);
locManager.requestSingleUpdate("network",locListener,Looper.myLooper());
are not giving the same results with the emulator and the device !
Questions
If requestSingleUpdate is called many times (with different providers), only the last query will be taken ?
requestSingleUpdate must be called on ui thread ? If not, there's a trick to call it outside ?
Complete Class :
public class LocationNew {
private Context context;
public LocationNew(Context context) {
this.context = context;
}
public void requestNew(Looper paramLooper){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int permissionLocation = ContextCompat.checkSelfPermission(DooodApp.getContext(), Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionLocation != PackageManager.PERMISSION_GRANTED) {
return;
}
}
LocationManager locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
List<String> listProv = locManager.getProviders(true);
for (String provTmp: listProv) {
locManager.requestSingleUpdate(provTmp,locListener,Looper.getMainLooper());
}
}
private LocationListener locListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
Toast.makeText(context, "New location from : " + location.getProvider(), Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(context, "onStatusChanged" , Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(context, "onProviderEnabled" , Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(context, "onProviderDisabled" , Toast.LENGTH_SHORT).show();
}
};
}
Thank's to all.

Android: requestLocationUpdates

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) {}
}
}

LocationListener not working, without any error in LogCat

im a bit desperate because somehow, my code just stopped working.
Code:
public class MainBoard extends FragmentActivity implements OnClickListener, OnTouchListener, LocationListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
//Location Listener
current_location = new MarkerOptions();
current_location.icon(BitmapDescriptorFactory.fromResource(R.drawable.location));
LocationManager locationManager = (LocationManager)getSystemService(getApplicationContext().LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);
}
//LocationListener
#Override
public void onLocationChanged(Location location) {
Log.d("LocationListener", "LocationChanged");
updateCurrentLocation(location);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) {
Log.d("LocationListener", "onProviderEnabled");
instantiatePosition();
}
#Override
public void onProviderDisabled(String provider) {
Log.d("LocationListener", "onProviderDisabled");
showGPSOfflineAlert();
}
}
I can't really see the error here, LogCat doesn't report any error, but no method is called, not even onProviderEnabled or onProviderDisabled...
Thanks in advance :)

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) {
}
};

Categories

Resources