Reputation: 37
I try to find location using GPS_Provider
in android. when try to get location using Network_Provider
than it give me location and call callback function properly, but when I tried to get location using GPS_Provider
than it give me location null & also not calling callback function onLocationChanged
of LocationListener
.
My code is:
LocationManager lm;
boolean isNetwork = false;
Location location;
LocationListener locationListener;
LocationListener loc;
MyLocation myLocation;
Location location1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
try {
myLocation = new MyLocation();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
Log.e("Location", "Permission Not Given");
return;
}
Log.e("Location", "Permission is Given");
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocation);
location1=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}catch(Exception e)
{
}
}
MyLocation.class
public class MyLocation implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if(location!=null)
{
String lat,Longi;
lat=String.valueOf(location.getLatitude());
Longi=String.valueOf(location.getLongitude());
Log.e("Latitude: " , lat);
Log.e("Longitude: " , Longi);
}
else
Log.e("Location is null", "Location not Found");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e("Latitude: " ,""+ provider+ " Status: "+status);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Log.e("Latitude: " , provider);
}
}
It give me logcat error as follow:
02-04 00:50:52.280 14480-14480/com.arc.locationusingnetwork E/Location: Permission is Given
02-04 00:50:52.360 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 46
02-04 00:50:52.370 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 49
02-04 00:50:52.370 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-04 00:50:52.380 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-04 00:50:52.380 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-04 00:50:52.390 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 52
Upvotes: 1
Views: 4255
Reputation: 11545
You need to move your device to call onLocationChanged
of LocationListener
as
LocationListener
is Used for receiving notifications from the LocationManager
when the location has changed. These methods are called if the LocationListener
has been registered with the location manager service using the LocationManager.requestLocationUpdates(String, long, float, LocationListener)
method.
Now there is no guarantee that getLastKnownLocation
always return Location
as it returns last location but if app is installed freshly or user did clear data then gps last known location returns null
so to handle this you should move your device so that onLocationChanged
is called ( I recommend to move in open so that device connect to gps fast).
OR
You can use FusedLocationProviderApi
as it is the latest API and the best among the available possibilities to get location in Android.
google docs : Getting the Last Known Location
Upvotes: 0
Reputation: 851
Have you checked if your GNSS/GPS is Enabled?
boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Upvotes: 0
Reputation: 456
The old LocationListener class is probably deprecated. You should use FusedLocationApi by Android to fetch current location or receive location updates via FusedLocationAPI's LocationListener.
It's a good tutorial for using FusedLocationApi : Android Location API
Upvotes: 2