2014-11-13 4 views
-1

Может ли кто-нибудь найти рабочее решение для guraneteed, чтобы вызвать onLocationChanged (...) метод LocationListener? Похоже, что многие люди сталкиваются с этой проблемой. https://code.google.com/p/android/issues/detail?id=57707 мое внедрение не является вообще вызывался даже после того, как пробовать и пробовать много способов ...Android LocationListner ... Feeling Sucked :(

public class MyLocationListener implements LocationListener{ 
    final String _logTag = "Location listener"; 

    public void onLocationChanged(Location location) { 
     String provider = location.getProvider(); 
     double lat = location.getLatitude(); 
     double lng = location.getLongitude(); 
     float accuracy = location.getAccuracy(); 
     long time = location.getTime(); 
Log.d("long"," "+lng); 
     String logMessage = LogHelper.formatLocationInfo(provider, lat, lng, accuracy, time); 
     Log.d(_logTag, "Monitor Location:" + logMessage); 
    } 

    public void onStatusChanged(String s, int i, Bundle bundle) { 
     String statusMessage = LogHelper.translateStatus(i); 
     // Log.d(_logTag, "Monitor Location - Status:" + statusMessage); 
     Set<String> keys = bundle.keySet(); 
     for(String key:keys){ 
      Log.d(_logTag, "Monitor Location - Bundle Key:" + key); 
     } 
    } 

    public void onProviderEnabled(String s) { 
     Log.d(_logTag, "Monitor Location - Provider ENABLED by USER: " + s); 
    } 

    public void onProviderDisabled(String s) { 
     Log.d(_logTag, "Monitor Location - Provider DISABLED by USER: " + s); 
    } 
} 
+0

Почему вы не изменить название, чтобы чувствовать себя лучше? – danny117

+0

hehehe sure @ danny117, как только я получу задачу. – andaspire

ответ

0

Это то, что я использовал в прошлом, она работает очень хорошо для меня:

public class LocationService 
    extends Service implements GooglePlayServicesClient.ConnectionCallbacks, 
     GooglePlayServicesClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener { 

    private LocationReceiver locationReceiver = new LocationReceiver(); 
    public LocationClient locationClient; 
    public LocationRequest locationRequest; 
    private Location previous; 
    String TAG = "LocationService"; 
    private boolean monitorLocation; 
    private boolean googleServicesAvailable = false; 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     this.monitorLocation = false; 
     //this.locationListener = new LocationListener(); 
     this.locationRequest = LocationRequest.create(); 
     this.locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); // Use high accuracy 
     this.locationRequest.setInterval(Singletons.Location.regularIntervalTime); // Setting the update interval to 5mins 
     this.locationRequest.setFastestInterval(Singletons.Location.fastIntervalTime); // Set the fastest update interval to 1 min 
     this.googleServicesAvailable = servicesConnected(); 
     this.locationClient = new LocationClient(this, this, this); 
    } 
    public boolean servicesConnected() { 
     int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     if (resultCode == ConnectionResult.SUCCESS) { 
      //Log.d(TAG, "LocationService: Location services available"); 
      return true; 
     } else { 
      Log.e(TAG, "Location services not available"); 
      return false; 
     } 
    } 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     super.onStartCommand(intent, flags, startId); 
     if (!this.googleServicesAvailable || this.locationClient.isConnected() || this.monitorLocation) { 
      //Log.d(TAG, "LocationService: services already started"); 
      return START_STICKY; 
     } 
     setUpLocationClientIfNeeded(); 
     if (!this.locationClient.isConnected() || !this.locationClient.isConnecting() && !this.monitorLocation) { 
      //Log.d(TAG, "LocationService: service is starting now"); 
      this.monitorLocation = true; 
      this.locationClient.connect(); 
     } 
     return Service.START_STICKY; 
    } 
    public void setUpLocationClientIfNeeded() { 
     if (this.locationClient == null) { 
      this.locationClient = new LocationClient(this, this, this); 
     } 
    } 
    @Override 
    public void onLocationChanged(Location location) { 
     if (this.previous == null) { 
      this.previous = location; 
     } 
     if (!this.isSameLocation(location, this.previous)) { 
      LocationDataSource datasource = new LocationDataSource(getApplicationContext()); 
      datasource.open(); 
      datasource.insertNewLocation(location, this.previous); 
      datasource.close(); 
      this.previous = location; 
     } 
    } 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 
    @Override 
    public void onConnected(Bundle bundle) { 
     this.locationClient.requestLocationUpdates(this.locationRequest, this); 
    } 
    @Override 
    public void onDisconnected() { 
     this.monitorLocation = false; 
     this.locationClient = null; 
    } 
    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     this.monitorLocation = false; 
     if (connectionResult.hasResolution()) { 
      Log.d(TAG, "LocationService: We can resolve the problem?"); 
      // If no resolution is available, display an error dialog 
     } else { 
      Log.e(TAG, "LocationService: We cannot resolve the gms location problem"); 
     } 
    } 
    @Override 
    public void onDestroy() { 
     // Turn off the request flag 
     this.monitorLocation = false; 
     if (this.googleServicesAvailable && this.locationClient != null) { 
      //this.locationClient.removeLocationUpdates(this); 
      // Destroy the current location client 
      this.locationClient = null; 
     } 
     super.onDestroy(); 
    } 
    public boolean isSameLocation(Location l, Location p) { 
     return (p.getLongitude() == l.getLongitude()) && (p.getLatitude() == l.getLatitude()); 
    } 
} 
+0

Я еще не пробовал ... но на самом деле так много! :) @ lilott8 – andaspire

 Смежные вопросы

  • Нет связанных вопросов^_^