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

时间:2013-11-30 14:09:55

标签: android gps location

您好我的班级会查找您的位置,首先是网络提供商,然后等待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();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

使用google play服务更简单