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 is not called automatically
Problem seems to be solved.
In onCreate, I added:
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
best = mgr.getBestProvider(crit, false);
mgr.requestLocationUpdates(best, 0, 1, locationListener);
onLocationChanged now looks like that:
@Override
public void onLocationChanged(Location location) {
i++;
nextPoint = overlay.getMyLocation();
latitude = nextPoint.getLatitudeE6();
longtitude = nextPoint.getLongitudeE6();
lastPoint = new GeoPoint((int) latitude, (int) longtitude);
Points.add(lastPoint);
MapOverlay mapOverlay = new MapOverlay(Points.get(i - 1), Points.get(i));
map.getOverlays().add(mapOverlay);
mMapController.animateTo(Points.get(i));
nextPoint = null;
lastPoint = null;
}
Also, very important methods:
@Override
protected void onResume() {
super.onResume();
mgr.requestLocationUpdates(best, 10000, 1, locationListener);
}
@Override
protected void onPause() {
super.onPause();
mgr.removeUpdates(locationListener);
}
And also some new permissions:
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
onLocationChanged() callback is never called until I turn on my device location tracker manually
If you want to track location on android you have to turn on tracker in settings. As far as I know there is no way to do it automatically.
So answering your question directly, there is no way to do it. Standard solution is to ask user to turn tracking on and open settings menu.
For example:
/**
* Function to show settings alert dialog
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// Setting Icon to Dialog
//alertDialog.setIcon(R.drawable.delete);
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
This is the link to the tutorial:
http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
Callback method onLocationChanged() is not working on viewmodel
In you view model
public class MAgencyViewModel extends AndroidViewModel {
private LocationCallback locationCallback;
public boolean requestingLocationUpdates;
public MAgencyViewModel(Application app) {
super(app);
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
for (Location location : locationResult.getLocations()) {
// Update UI with location data
// ...
}
}
};
}
public void startLocationUpdates() {
requestingLocationUpdates = true
LocationServices.getFusedLocationProviderClient(getApplication())
.requestLocationUpdates(locationRequest,
locationCallback,
Looper.getMainLooper());
}
public void stopLocationUpdates() {
requestingLocationUpdates = false
LocationServices.getFusedLocationProviderClient(getApplication())
.removeLocationUpdates(locationCallback);
}
}
Now in your Fragment/Activity override on pause and onresume
Then call the startlocation and stoplction in your viewmodel
@Override
protected void onPause() {
super.onPause();
viewModel.stopLocationUpdates();
}
@Override
protected void onResume() {
super.onResume();
if (viewModel.requestingLocationUpdates) {
startLocationUpdates();
}
Notice setting and checking boolean requestingLocationUpdates if locationRequest is active
Related Topics
Check for Access to Notifications Using Notificationlistenerservice
How to Enable Internal App Sharing for Android
Successful Share Intent for Android
Should I Use Getapplicationcontext or Activity.This in a Long Running Asynctask
Android - Save Images in an Specific Folder
How to Use Standard Attribute Android:Text in My Custom View
Easier Way to Get View's Id (String) by Its Id (Int)
How to Call a Wcf Service Using Ksoap2 on Android
When Using Alertdialog.Builder with Edittext, The Soft Keyboard Doesn't Pop
How to Fetch Data from a Web Server in an Android Application
How to Install/Update/Remove APK Using "Packageinstaller" Class in Android L
How to Create Listview Inside Dialog
How to Get Spinner Selected Item Value to String
Notificationlistenerservice Implementation
Change The Background Color of Cardview Programmatically
How to Enable Zoom Controls and Pinch Zoom in a Webview
Onoptionsitemselected Not Called When Using Actionlayout (Sherlockactionbar)