0

After registering with LocationManager for LocationManager.NETWORK_PROVIDER , the callback onStatusChanged() is never called even when data or network is switched on/off during registered period. Is this method not valid for Network based providers ?

S.D.
  • 29,290
  • 3
  • 79
  • 130
  • Which android API ? Does it work for GPS ? – Mr_and_Mrs_D May 26 '13 at 10:47
  • @Mr_and_Mrs_D Last checked on Jelly Bean. Yes it works for GPS, only NETWORK_PROVIDER didn't change its status. – S.D. May 26 '13 at 11:29
  • Strange - cause GPS is [reported](http://stackoverflow.com/questions/2021176/how-can-i-check-the-current-status-of-the-gps-receiver/3712727#3712727) as not working on older devices/APIs (?) - also see [here](http://code.google.com/p/android/issues/detail?id=9433). Maybe post an issue (and add it here somewhere) ? – Mr_and_Mrs_D May 26 '13 at 11:39
  • @Mr_and_Mrs_D I have not tested on older versions, but on my Jelly bean device, GPS_PROVIDER calls `onStatusChanged()` immediately when I turn it off or on. – S.D. May 26 '13 at 11:58
  • Ok - maybe post relevant parts of the code then ? – Mr_and_Mrs_D May 26 '13 at 13:02
  • @Mr_and_Mrs_D [Here's the source of location class](https://bitbucket.org/zedvoid/indico/src/26f460a2d08e/src/com/zedvoid/Indico/common/PositionManager.java?at=master). It is a reusable class for location fix with two start/stop methods, and getters for location and provider status. – S.D. May 26 '13 at 17:51
  • Could [this](http://stackoverflow.com/a/15840252/281545) be the issue ? The code seems ok – Mr_and_Mrs_D May 26 '13 at 23:29

1 Answers1

0

Try this :

package com.mytest;

import android.R;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

public class NetworkTest extends Activity {

    private LocationManager locationManager;
    private TextView textView;
    private final LocationListener networkLocationListener=new LocationListener(){

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            switch (status) {
            case LocationProvider.AVAILABLE:
                textView.setText(textView.getText().toString()
                        + "Network location available again\n");
                break;
            case LocationProvider.OUT_OF_SERVICE:
                textView.setText(textView.getText().toString()
                        + "Network location out of service\n");
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                textView.setText(textView.getText().toString()
                        + "Network location temporarily unavailable\n");
                break;
            }
        }

        @Override
        public void onProviderEnabled(String provider) {
            textView.setText(textView.getText().toString()
                    + "Network Provider Enabled\n");
        }

        @Override
        public void onProviderDisabled(String provider) {
            textView.setText(textView.getText().toString()
                    + "Network Provider Disabled\n");
        }

        @Override
        public void onLocationChanged(Location location) {
            textView.setText(textView.getText().toString()
                    + "New network location: "
                    + String.format("%9.6f", location.getLatitude()) + ", "
                    + String.format("%9.6f", location.getLongitude()) + "\n");
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitynetwork);
        textView = (TextView) findViewById(R.id.textview);
        locationManager = (LocationManager)
                getSystemService(Context.LOCATION_SERVICE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 5000, 0,
                networkLocationListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(networkLocationListener);
    }
}

It works for me.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
  • Thanks for your effort, but no, `onStatusChanged()` is not being called when I turn network on/off or switch to airplane mode and back. – S.D. Jan 31 '13 at 19:07
  • @Singularity will it be okay if you go with `Broadcast Receiver` for monitoring network changes? – Pratik Sharma Jan 31 '13 at 19:28
  • I can do that, or may be another workaround. What I think is that if Android lists network as one of the providers, it must also update me about its status, like it does with GPS etc. – S.D. Jan 31 '13 at 19:50