网络位置提供商的状态永远不会改变?

时间:2013-01-31 17:34:28

标签: android geolocation

LocationManager注册LocationManager.NETWORK_PROVIDER后,即使在注册期间打开/关闭数据或网络,也不会调用回调onStatusChanged()。此方法对基于网络的提供商无效吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

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);
    }
}

它对我有用。