OnLocationChanged callback is never called
It looks like you setup should work, since it doesn't, I would make your example as simple as possible in order to troubleshoot. I would make your request look like
requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
This way you get ALL the updates possible. And comment out the part about getting the last known location. It's not needed yet.
Then in onLocationChanged()
, just have
Log.v(TAG, "IN ON LOCATION CHANGE, lat=" + latitude + ", lon=" + longitude);
Comment out the rest so that you keep your listener active. This should give you a stream of updates on a real device. On the emulator, you'll need to use DDMS, and you'll get one GPS update each time you press send.
onLocationChanged() never called
Do you never get a location or only as you write in your comment "sometimes it gets location but not at the time of click."?
Might be that your code is faster than LocationManager, which might not yet have called onLocationChanged(). Change your code in order to get the last known location, or wait until your locListener was called:
locManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 1000, 1, locListener);
mobileLocation = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mobileLocation != null) {
onlocationchanged not called in Android
use this code worked in all devices and tested:
private void getCurrentLocation() {
// Log.e("getLocation","Called");
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
mlat = location.getLatitude();
mlon = location.getLongitude();
String mLogn = Double.toString(mlon);
String mLatd = Double.toString(mlat);
// Log.e("mLogn",""+mLogn);
// Log.e("mLatd",""+mLatd);
mLogn=mLogn.trim();
mLatd=mLatd.trim();
if (mLatd==null || mLogn==null) {
if (mLatd.isEmpty()) {
mLatd = "No data found";
}
if (mLogn.isEmpty()) {
mLogn = "No data found";
}
}
// Log.e("Location",""+mlon+" "+mlat);
// Toast.makeText(CampaignTrain.this, "your location is " + mLogn + " " + mLatd, Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
String locationProvide = LocationManager.NETWORK_PROVIDER;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},COARSE_LOCATION_REQUEST_CODE);
return;
}
}
if ( ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},FINE_LOCATION_REQUEST_CODE);
return;
}
}
locationManager.requestLocationUpdates(locationProvide, 0, 0, locationListener);
Location lastLocation=locationManager.getLastKnownLocation(locationProvide);
}
and set the runtime permission :
int FINE_LOCATION_REQUEST_CODE=101;
int COARSE_LOCATION_REQUEST_CODE=102;
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode)
{
case COARSE_LOCATION_REQUEST_CODE:
{
if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED)
{
}
else
{
mLatd="User denied location";
mLogn="User denied location";
}
return;
}
case FINE_LOCATION_REQUEST_CODE:
{
if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED)
{
}
else
{
mLatd="User denied location";
mLogn="User denied location";
}
return;
}
}
}
OnLocationChanged is never called
I found a problem. GPS in my phone isn't working. If I use only the network provider it works. Other GPS apps can't get any location from gps too.
Related Topics
Error: Resource Android:Attr/Fontvariationsettings Not Found
Android Material: Status Bar Color Won't Change
How to Get Name of Wifi-Network Out of Android Using Android API
Change Actionbar Color Programmatically More Than Once
How to Make an Http Request Using Cookies on Flutter
How to Get the Width and Height of an Image View in Android
When to Clear the Cache Dir in Android
Design Layout for Multiple Screens
Menuitemcompat.Getactionview Always Returns Null
Gradle Version 2.2 Is Required. Current Version Is 2.10
Getapplicationcontext(), Getbasecontext(), Getapplication(), Getparent()
How to Limit the Height of Spinner Drop Down View in Android
How to Send Broadcast from One App to Another App
Checkselfpermission Method Is Not Working in Targetsdkversion 22
Android Mediaplayer Throwing "Prepare Failed.: Status=0X1" on 2.1, Works on 2.2
Passing Strings Between Activities in Android