2013-07-26 62 views
0

我有以下位置列表以獲取用戶當前位置。在放棄之前,我大約等待45秒,同時使用網絡和GPS。問題是,有時(大約15%的時間)即使在45秒後我也會得到空位。發生這種情況時我沒有發現任何模式。任何人都可以闡明可能發生的事情嗎?Android LocationListner無法找到我的位置

public class MyLocationListener implements LocationListener { 

private static final float DELTA_ACURACIDADE = 30; 

public static final long DEFAULT_TIME_UPDATES = 0; 

public static final float DEFAULT_DISTANCE_UPDATES = 0; 

private static final long TIME_CONSIDERED_OLD_LOCATION = 1000 * 150; 

private Location location; 

@Override 
public void onLocationChanged(Location newLocation) { 

    if (newLocation != null && newLocation.getLatitude() != 0.0d && newLocation.getLongitude() != 0.0d) { 

     boolean isOk = false; 

     if (this.location == null) { 
      this.location = newLocation; 
     } else { 

      long deltaTime = newLocation.getTime() - this.location.getTime(); 

      boolean isNewer = deltaTempo > 0; 

      boolean isAlotNewer = deltaTempo > TIME_CONSIDERED_OLD_LOCATION; 

      int deltaAcuracidade = (int) (this.location.getAccuracy() - newLocation.getAccuracy()); 
      boolean isEqualOrMoreAccurate = deltaAcuracidade >= DELTA_ACURACIDADE; 

      if ((isNewer && isEqualOrMoreAccurate) || isAlotNewer) { 
       aceitarNovaLocation = true; 
      } 

      if (aceitarNovaLocation) { 
       this.location = newLocation; 

      } 
     } 
    } 
} 

public Location getLocation() { 
    return this.location; 
} 

@Override 
public void onProviderDisabled(String provider) { 
} 

@Override 
public void onProviderEnabled(String provider) { 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

} 





public class checkInActivity extends Activity { 

private long WAIT_FOR_LOCATION = 1000 * 40; 

private Location roteiroLocation; 

private MyLocationListener locationListner; 

private LocationManager locationManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    this.locationListner = new MyLocationListener(); 

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MyLocationListener.DEFAULT_TIME_UPDATES, 
      MyLocationListener.DEFAULT_DISTANCE_UPDATES, this.locationListner); 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
      MyLocationListener.DEFAULT_TIME_UPDATES, MyLocationListener.DEFAULT_DISTANCE_UPDATES, 
      this.locationListner); 

} 

protected void processLocation() { 

    new AsyncTask<Void, Void, Void>() { 

     private boolean wasLocationAccepted = false; 

     private ProgressDialog dialog = new ProgressDialog(checkInActivity.this); 

     @Override 
     protected void onPreExecute() { 

      this.dialog.setMessage("message"); 
      this.dialog.setCancelable(false); 
      this.dialog.show(); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 

      Long t = Calendar.getInstance().getTimeInMillis(); 

      Location location = null; 

      while (Calendar.getInstance().getTimeInMillis() - t < WAIT_FOR_LOCATION) { 

       location = locationListner.getLocation(); 

       if (location != null && location.getAccuracy() < MIN_ACCURACY_LOCATION 
         && location.distanceTo(place) < MIN_ACCURACY_LOCATION) { 
        wasLocationAccepted = true; 
        break; 
       } 

       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 

      } 

      locationManager.removeUpdates(locationListner); 

      if (this.dialog.isShowing(){ 
       this.dialog.dismiss(); 
      } 

      SigoMobileHelper.performOnBackgroundThread(runnable); 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      Log.i(TAG, "AsyncTask.onPostExecute"); 

      finish(); 
     } 

    }.execute(); 

} 

@Override 
protected void onStart() { 
    processLocation(); 
    super.onStart(); 
} 

} 

回答

1

您的代碼就可以了。

您沒有獲取位置信息,因爲可能存在低GPS信號或衛星沒有應答,應用無法下載位置。

+0

但我同時登錄GPS和網絡提供商。如果我沒有獲得GPS,我應該至少獲得一個網絡位置。 (我在有手機信號的地方測試過) –