Android应用偶尔无法获取位置

时间:2013-11-15 04:32:57

标签: android gps location

我遇到了偶尔无法获得位置的问题。虽然它无法在我自己的Android应用上获得位置,但谷歌地图仍然可以找到当前位置。杀死并重启我的应用程序后仍然存在问题,但重启电话后可以解决。

有人可以帮忙吗?非常感谢!

以下是您参考的源代码的一部分:

private LocationManager lm;
private boolean gps_enabled=false;
private boolean network_enabled=false;
private boolean got_location=false;
private static long location_time_threshold = 300000; //300000milliseconds = 5mins

private boolean getLocation() {
    if(lm==null)
        lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
    try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}

    if(!gps_enabled && !network_enabled) {
        System.out.println("failed to get from GPS and Network ");
        //show err message box
        return false;
    }

    if (!getLastLocation()) {
        if(network_enabled)
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        if(gps_enabled)
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);

        System.out.println("getLocation Lat " + userLat + " Long " + userLong);
        handler.postDelayed(delayForCheck, 10000);
    }

    return true;
}

private Runnable delayForCheck = new Runnable() {
    public void run() {
        if (!got_location){
            if (!getLastLocation()) {
                System.out.println("delayForCheck failed to get from GPS and Network ");
                if (!network_enabled) {
                    //show err message box
                } else {
                    //show err message box
                }
            }
        }
    }
}; 


LocationListener locationListenerGps = new LocationListener() {
    public void onLocationChanged(Location location) {
        lm.removeUpdates(this);
        lm.removeUpdates(locationListenerNetwork);
        got_location = true;
        userLat = location.getLatitude();
        userLong = location.getLongitude();
        System.out.println("locationListenerGps: Lat " + userLat + " Long " + userLong);
    }
    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
};

LocationListener locationListenerNetwork = new LocationListener() {
    public void onLocationChanged(Location location) {
        lm.removeUpdates(this);
        lm.removeUpdates(locationListenerGps);
        got_location = true;
        userLat = location.getLatitude();
        userLong = location.getLongitude();
        System.out.println("locationListenerNetwork: Lat " + userLat + " Long " + userLong);
    }
    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
};

private boolean getLastLocation() {
    lm.removeUpdates(locationListenerGps);
    lm.removeUpdates(locationListenerNetwork);

    Location net_loc=null, gps_loc=null;
    if(gps_enabled)
        gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if(network_enabled)
        net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    if(gps_loc!=null && net_loc!=null){
        if ((new Date().getTime() - net_loc.getTime()) < location_time_threshold 
            || (new Date().getTime() - gps_loc.getTime()) < location_time_threshold ) {
            if(gps_loc.getTime()>net_loc.getTime()) {
                userLat = gps_loc.getLatitude();
                userLong = gps_loc.getLongitude();
            } else {
                userLat = net_loc.getLatitude();
                userLong = net_loc.getLongitude();
            }
            got_location = true;
            System.out.println("getLastKnownLocationLAST: Lat " + userLat + " Long " + userLong);
            return true;
        }
    } else if(net_loc!=null){
        if ((new Date().getTime() - net_loc.getTime()) < location_time_threshold) {
            userLat = net_loc.getLatitude();
            userLong = net_loc.getLongitude();
            got_location = true;
            System.out.println("getLastKnownLocationNetwork: Lat " + userLat + " Long " + userLong);
            return true;
        }
    } else if(gps_loc!=null){
        if ((new Date().getTime() - gps_loc.getTime()) < location_time_threshold) {
            userLat = gps_loc.getLatitude();
            userLong = gps_loc.getLongitude();
            got_location = true;
            System.out.println("getLastKnownLocationGPS: Lat " + userLat + " Long " + userLong);
            return true;
        }
    }
    return false;
}

0 个答案:

没有答案