与互联网服务提供商(网络提供商)的位置



你好,我有一个类可以查找您的位置,首先是网络提供商,然后等待GPS提供商的信息。我不知道为什么,但我从来没有从网络提供商那里得到信息,当信号被获取时,GPS工作得很好。

public class GpsListener {
    public static GpsListener refrence = null ;
    public LocationManager locationManager = null;
    public LocationListener locationListener = null;
    public Location location = null;
    public static GpsListener getInstance(){
        if(refrence == null){
            refrence = new GpsListener();
        }
        return refrence;
    }
    public void startGpsCallBack(Context activityContext){
        locationManager = (LocationManager) activityContext.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new mylocationlistener();
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        location = locationManager
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            Map.MyPositionlatitude = location.getLatitude();
            Map.MyPositionlongitude = location.getLongitude();
        }
    }
    public class mylocationlistener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                  Map.MyPositionlatitude = location.getLatitude();
                  Map.MyPositionlongitude = location.getLongitude();
            }
        }
        @Override
        public void onProviderDisabled(String provider) {
        }
        @Override
        public void onProviderEnabled(String provider) {
        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }
    public void stopGpsCallBack(){
        if (locationManager != null) {
            locationManager.removeUpdates(locationListener);
        }
    }
    public void startGpsCallbackAgain(Context activityContext){
        locationManager = (LocationManager) activityContext.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new mylocationlistener();
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        location = locationManager
        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
              Map.MyPositionlatitude = location.getLatitude();
              Map.MyPositionlongitude = location.getLongitude();
        }
    }
}

使用谷歌播放服务更简单的

最新更新