2012-10-17 36 views
2
package com.ecsmon.android.core; 

import static com.ecsmon.android.constants.Constants.log; 

import java.io.IOException; 
import java.util.List; 

import android.annotation.SuppressLint; 
import android.content.Context; 
import android.location.Address; 
import android.location.Criteria; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 

@SuppressLint("NewApi") 
public class GPSManager { 

    private double currentLatitude = 0d; 
    private double currentLongitude = 0d; 
    private static Context mCtx; 
    private Location lastLocationBestProvider = null; 
    private LocationManager mLocationManager; 
    private GPSListenerImpl mGPSListener; 
    private com.ecsmon.android.core.LocationListener mOutListener; 
    private boolean enabled = false; 
    private GPSListenerImpl mNETListener; 

    public GPSManager(Context ctx, com.ecsmon.android.core.LocationListener locationListener) { 
    mCtx = ctx; 
    mLocationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE); 
    mOutListener = locationListener; 
    } 

    /** 
    * Start location updates 
    */ 
    public void start() { 
    log("#### Started tracking"); 
    lastLocationBestProvider = getLastLocationFromBestProvider(); 
    if (lastLocationBestProvider != null) { 
     currentLatitude = lastLocationBestProvider.getLatitude(); 
     currentLongitude = lastLocationBestProvider.getLongitude(); 
     log("lat" + currentLatitude + " long " + currentLongitude); 
    } else { 
     log("last loaction is null"); 
    } 
// mGPSListener = new GPSListenerImpl("GPS"); 
    mNETListener = new GPSListenerImpl("NET"); 

    mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 1, mNETListener); 
// mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, mGPSListener); 

    } 

    private class GPSListenerImpl implements LocationListener { 
    private String name = ""; 
    public GPSListenerImpl(String name) { 
     log("listener created" + name); 
     this.name = name; 
    } 

    public void onLocationChanged(Location loc) { 
     log("######### LOCATION CHANGED CALLED!!!!!!!!!!!!!!!!! ##############"); 
     if (loc != null) { 
     log("######### location changed " + loc.getAccuracy()); 
     currentLatitude = loc.getLatitude(); 
     currentLongitude = loc.getLongitude(); 
     mOutListener.update(currentLongitude, currentLatitude); 
     } else { 
     log("location is null"); 
     } 
    } 

    public void onProviderDisabled(String provider) { 
     log("provider disabled > " + name); 
    } 

    public void onProviderEnabled(String provider) { 
     log("provider enabled > " + name); 
    } 

    public void onStatusChanged(String provider, int status, Bundle extras) { 
     log("status changed"); 
    } 
    } 

    /** 
    * Return last location saved in phone or null 
    * 
    * @return Location 
    */ 
    public Location getLastLocationFromBestProvider() { 
    if (!enabled) { 
     return null; 
    } 
    try { 
     LocationManager lm = (LocationManager) mCtx.getSystemService(Context.LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
     criteria.setAltitudeRequired(false); 
     criteria.setBearingRequired(false); 
     criteria.setCostAllowed(true); 
     String strLocationProvider = lm.getBestProvider(criteria, true); 
     Location location = lm.getLastKnownLocation(strLocationProvider); 
     if (location != null) { 
     return location; 
     } 
     return null; 
    } catch (Exception e) { 
     log(e.getMessage()); 
     return null; 
    } 
    } 

    /** 
    * Returns human readable address from longitude and latitude 
    * 
    * @param latitude 
    * @param longitude 
    * @return 
    */ 
    public String getAddress(Double latitude, Double longitude) { 
    if (!enabled) { 
     return null; 
    } 
    String m = ""; 
    try { 
     if (!Geocoder.isPresent()) { 
     return null; 
     } 
     Geocoder geo = new Geocoder(mCtx); 
     List<Address> addresses = geo.getFromLocation(latitude, longitude, 1); 
     if (addresses.isEmpty()) { 
     return null; 
     } else { 
     if (addresses.size() > 0) { 
      m = addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() + ", " 
       + addresses.get(0).getCountryName(); 
     } 
     } 
    } catch (IOException ie) { 
     log("No connection."); 
     return null; 
    } catch (Exception e) { 
     log("Can't read adress from this cordinates : lat = " + latitude + " long " + longitude); // 
     return null; 
    } 
    return m; 
    } 

    /** 
    * Removes all location updates 
    */ 
    public void stop() { 
    try { 
     mLocationManager.removeUpdates(mGPSListener); 
     mLocationManager.removeUpdates(mNETListener); 
    } catch (Exception e) { 

    } 

    } 

} 

這是我的主要類獲取當前位置和onLocationChanged永遠不會被調用。我在模擬器上測試,並通過模擬器控制發送模擬經度和緯度。請幫助這是推動我瘋了:(onLocationChanged永遠不會被調用模擬器

+1

你授予在清單接收模擬位置的權限? – Stefan

+0

起初,我沒有,但現在加入後,我發現模擬器發送模擬位置後崩潰 –

+0

這次崩潰的堆棧跟蹤說什麼? – Stefan

回答

0

問題是模擬器它不會工作,但在真機上它發現秒我的位置

1

可以使用Emulator control改變模擬器的GPS位置值。通過這樣做(onLocationChanged方法將工作),你可以在模擬器

+0

嘗試了很多次不開懷,但我通過使用真正的設備 –

+0

解決它但我檢查使用加載模擬器控制中的kml文件,它的工作原理:)我可以知道你使用哪個IDE? – ponraj

+0

我使用日食:) –

3

斯特凡是正確的測試應用程序。你需要做兩件事情。到模擬位置

  1. 授予訪問權限。截至目前,這需要在一個被指定特別mani fest文件在src/debug/AndroidManifest.xml下。創建XML和此權限添加到它:

使用許可權的android:NAME = 「android.permission.ACCESS_MOCK_LOCATION」

  • 確保場所的經理是大呼過癮到GPS提供商而不是網絡提供商。這些信息可以在這裏找到:
  • http://developer.android.com/guide/topics/location/strategies.html#MockData

    相關問題