slhddn slhddn - 10 months ago 71
Android Question

Android Location listener in Service does not work until I reactivate WiFi/mobile network

My location listener works normally, collects data without any problem. But sometimes it doesn't collect any data. I have to turn off and restart my location provider at this time. Restarting fixes the problem,however, it's probably not the best thing the user is expected to do.

When I use GPS as provider, there is no problem.

Location listener works in Service. I can't understand the problem. Is it about Android or my code?

Thanks in advance.

Answer Source

You could use new location provider (FusedLocationProvider), which combines info from different location providers, so if your device has any possibility to obtain location, You'll get know it. Of course, You should enable in preferences for your device to use location info by apps.

Check for extended info about this provider.

This is solution working fine for me:

public class FusedLocationListener implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener,  {

    public interface LocationListener {
        public void onReceiveLocation(Location location);

    private LocationListener mListener;

    public static final String TAG = "Fused";
    private LocationClient locationClient;
    private LocationRequest locationRequest;

    protected int minDistanceToUpdate = 1000;
    protected int minTimeToUpdate = 10*1000;

    protected Context mContext;

    public void onConnected(Bundle bundle) {
        Log.d(TAG, "Connected");
        locationRequest = new LocationRequest();
        locationClient.requestLocationUpdates(locationRequest, this);


    public void onDisconnected() {
        Log.d(TAG, "Disconnected");

    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.d(TAG, "Failed");

    private static FusedLocationListener instance;

    public static synchronized FusedLocationListener getInstance(Context context, LocationListener listener){
        if (null==instance) {
            instance = new FusedLocationListener(context, listener);
        return instance;

    private FusedLocationListener(Context context, LocationListener listener){
        mContext = context;
        mListener = listener;

    public void start(){

        Log.d(TAG, "Listener started");
        locationClient = new LocationClient(mContext,this,this);


    public void onLocationChanged(Location location) {
        Log.d(TAG, "Location received: " + location.getLatitude() + ";" + location.getLongitude());
        //notify listener with new location

    public void stop() {


public class MyActivity extends Activity implements FusedLocationListener.LocationListener {

    public void onCreate(Bundle savedInstanceState) {

      FusedLocationListener locationListener FusedLocationListener.getInstance(getApplicationContext(), this);             


    public void onReceiveLocation(Location location) {
       //handle location here