2013-08-05 40 views
1

我有我自己的位置類。我有一個可能性結果,當我使用我的課程搜索GPS位置時,我返回0和0。Android位置無法返回自定義類別的位置

而如果我搜索了getLastKnown功能在GPS定位經理,我得到的強制值:

public Location getGPSloc(Context c){ 
    isGPSavailable(c); 
    if(gps_enabled) 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListenerGps); 
    timerGPS = new Timer(); 
    timerGPS.schedule(new GetLastLocationGPS(), 45000); 
    return gpsLoc; 
} 

這是我從函數外部調用類的,我用this.getApplicationContext ()在通話中。這是GetLastLocationGPS():

class GetLastLocationGPS extends TimerTask { 
    @Override 
    public void run() { 
     lm.removeUpdates(locationListenerGps); 
     if(gps_enabled){ 
      gpsLoc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);} 
     else{ 
      gpsLoc = null; 
     } 

    } 
} 

這裏的聽衆:

LocationListener locationListenerGps = new LocationListener() { 
    @Override 
    public void onLocationChanged(Location location) { 
     timerGPS.cancel(); 
     gpsLoc = location; 
     lm.removeUpdates(this); 
    } 
    @Override 
    public void onProviderDisabled(String provider) {} 
    @Override 
    public void onProviderEnabled(String provider) {} 
    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) {} 
}; 

當我打電話MyLocationInstance.getGPSloc(this.getApplicationContext())的返回值始終爲0,0。我有一個強制值83 43,我發送到模擬器,並顯示如果我在LocationManagerInstance.getLastKnownLocation(LM.GPS)子;

我不知道爲什麼這種方法不會獲取位置。

是不是因爲理論上定時器在通話發生時仍在運行?這是我能拿出答案的唯一方法。任何其他想法?

+0

更新。我更改了代碼,以便gpsLoc在開始新位置請求之前定義爲來自GPS提供者的getLastKnownLocation。這將返回最後一個已知值的值。現在我想到了,儘管我可能想要將我的請求綁定到UI線程,以使onCreate方法停滯,直到GPSLoc完成搜索新位置。 – user2097211

回答

0

我不確定你的問題是什麼,但這是我的負載和功能完整的代碼。

由於此類從片段擴展,因此它可以作爲您的片段的父級。 有一個聽衆,你的班級可以在每個時間間隔後獲得新的位置。

public class LocationFinderFragment extends Fragment implements 
     GooglePlayServicesClient.ConnectionCallbacks, 
     GooglePlayServicesClient.OnConnectionFailedListener { 

    public interface OnNewLocationFoundListener { 
     public void OnNewLocationFound(Location location); 
    } 

    private static final String TAG = "LocationFinderFragment"; 
    private static final long DEFAULT_UPDATE_LOCATION_INTERVAL = 30 * 1000; // update every 30 seconds 
    private static final long DEFAULT_TERMINATE_SAT_FINDING = 1 * 60 * 60 * 1000; // for 1 hour 

    private OnNewLocationFoundListener listener; // Listener of fragments 
    private OnNewLocationFoundListener listener2; // Listener of MainFragmentHolder 
    private Context context; 
    private LocationClient mLocationClient; 
    private static double lat = 0; 
    private static double lng = 0; 
    private static long lastTime = 0; 

    private LocationListener mLocationListener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      if(location == null) 
       return; 

      if(lat == location.getLatitude() && lng == location.getLongitude()) { 
       Log.e(TAG, "location not changed."); 
       return; 
      } 

      lat = location.getLatitude(); 
      lng = location.getLongitude(); 
      Log.i(TAG, "Location changed to (" + lat + ", " + lng + ")"); 

      // Ask fragment to get new data and update screen 
      listener.OnNewLocationFound(location); 

      // Display toast notification every minute (instead of every 30 seconds) 
      DateTime time = new DateTime(); 
      long currentTime = time.getMillis(); 
      if(currentTime > lastTime + 1 * 60 * 1000) { 
       listener2.OnNewLocationFound(location); 
       lastTime = currentTime; 
      } 
     } 
    }; 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     try { 
      listener2 = (OnNewLocationFoundListener) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() + " must implement OnNewLocationFoundListener interface."); 
     } 

     Log.i(TAG, "Fragment attached to activity."); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     // Getting context 
     context = getActivity().getApplicationContext(); 

     // Get location manager 
     mLocationClient = new LocationClient(context, this, this); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 

     // Get location 
     if(mLocationClient != null) 
      mLocationClient.connect(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 

     // remove listener in order to save resource 
     if(mLocationClient != null) 
      mLocationClient.disconnect(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 

     // remove listener in order to save resource 
     if(mLocationClient != null) 
      mLocationClient.disconnect(); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     Log.i(TAG, "Location Connected."); 
     // Get last known location 
     Location lastLocation = mLocationClient.getLastLocation(); 
     mLocationListener.onLocationChanged(lastLocation); 

     if(!SpGodMode.isLocationModeEnabled(context)) { 
      // Create location request 
      LocationRequest locationRequest = LocationRequest.create() 
        .setInterval(DEFAULT_UPDATE_LOCATION_INTERVAL) 
        .setExpirationDuration(DEFAULT_TERMINATE_SAT_FINDING) 
        .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
      mLocationClient.requestLocationUpdates(locationRequest, mLocationListener); 
     } else { 
      String strLoc = SpGodMode.getLocation(context); 
      if(strLoc != null) { 
       String lat = strLoc.substring(0, strLoc.indexOf(",")); 
       String lng = strLoc.substring(strLoc.indexOf(",") + 1); 
       Location location = new Location(""); 
       try { 
        location.setLatitude(Double.parseDouble(lat)); 
        location.setLongitude(Double.parseDouble(lng)); 
        mLocationListener.onLocationChanged(location); 
       } catch (NumberFormatException e) { 
        Log.e(TAG, "Wrong lat/lng for parsing as Double."); 
        Toast.makeText(context, "Wrong lat/lng", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     } 
    } 

    @Override 
    public void onDisconnected() { 
     Log.i(TAG, "Location Disconnected."); 
     if(mLocationClient != null && mLocationClient.isConnected()) 
      mLocationClient.removeLocationUpdates(mLocationListener); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     Log.e(TAG, "Connection failed listener."); 
    } 

    public void setLocationListener(OnNewLocationFoundListener listener) { 
     this.listener = listener; 
    } 

    public void disconnectLocation() { 
     mLocationClient.disconnect(); 
    } 
} 
+0

SpGodMode是什麼類? – DannyThunder

+0

@DannyThunder對不起,令人困惑,這是一個設置類,我有測試目的。測試人員能夠對他們的位置進行硬編碼,然後應用根據此信息顯示其位置,而不是從GPS或網絡讀取位置。你可以忽略那些零件... – Hesam

+0

沒有進口陳述? – Gerry