2013-06-11 87 views
1

所以,我有一个Android应用程序,要求从LocationManager类的GPS。 我有一个想要使用该数据的Activity以及实现了LocationListener的自定义类。 我已经写了一些自定义方法来将GPS值返回给我的其他类。我应该从哪个类调用locationManger.removeUpdates()?

目前,我正在要求并发布我的Activity类的位置更新,而不是我的LocationListener类的实现......我认为这是正确的方法,但我只想获得任何反馈在生命周期等(实现LocationListener类不是一个Activity,所以我不认为我甚至可以称之为onPause()等?)

这是Activity

package com.jessescott.sonicity; 

import android.app.Activity; 
import android.content.Context; 
import android.view.View; 
import android.widget.TextView; 
import android.location.Location; 
import android.location.LocationManager; 
import android.location.LocationListener; 


public class PlayActivity extends Activity { 

    // GLOBALS 
    private static final String TAG = "SoniCity"; 
    LocationManager locationManager; 
    MyLocationListener locationListener; 

    TextView latitude, longitude; 
    TextView ActualLatitude, ActualLongitude; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.play_layout); 

     // GPS 
     locationListener = new MyLocationListener(this);  


     // TextViews 
     latitude = (TextView) findViewById(R.id.Latitude); 
     longitude = (TextView) findViewById(R.id.Longitude); 

     ActualLatitude = (TextView) findViewById(R.id.ActualLat); 
     ActualLatitude.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.v(TAG, "Asking For New Latitude"); 
       ActualLatitude.setText(locationListener.getCurrentLatitude()); 
      } 
     }); 

     ActualLongitude = (TextView) findViewById(R.id.ActualLon); 
     ActualLongitude.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.v(TAG, "Asking For New Latitude"); 
       ActualLongitude.setText(locationListener.getCurrentLongitude());  
      } 
     }); 

    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     // Stop GPS 
     locationManager.removeUpdates(locationListener); 
     locationManager = null; 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 5, locationListener); 
    } 

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

} /* */ 

。 ..这是LocationListener实施N:

package com.jessescott.sonicity; 

    import android.content.Context; 
    import android.location.Location; 
    import android.location.LocationListener; 
    import android.os.Bundle; 
    import android.util.Log; 

    class MyLocationListener implements LocationListener { 

     private static final String TAG = "SoniCity"; 
     float currentLatitude  = 0; 
     float currentLongitude = 0; 

     public MyLocationListener(Context context) { 
      super(); 
     } 

     // Define all LocationListener methods 
     public void onLocationChanged(Location location) { 
     currentLatitude = (float)location.getLatitude(); 
     currentLongitude = (float)location.getLongitude(); 


     } 

     public void onProviderDisabled (String provider) { 
     Log.v(TAG, "Provider is " + provider); 
     } 

     public void onProviderEnabled (String provider) { 
     Log.v(TAG, "Provider is " + provider); 
     } 

     public void onStatusChanged (String provider, int status, Bundle extras) { 
     Log.v(TAG, "Status is " + status); 
     } 

     // Custom Methods 

     public String getCurrentLatitude() { 
      String lat = Float.toString(currentLatitude); 
      return lat; 
     } 

     public String getCurrentLongitude() { 
      String lon = Float.toString(currentLongitude); 
      return lon; 
     } 

    } /* */ 

我想,因为我已经把它移到一个单独的类(他们曾经是同一个),我只是想确保我打电话次请求/在正确的地方删除。

回答

1

我没有详细看过你的代码,但从我看到的对我来说看起来很好。 onResume()是注册听众的好地方,onPause()是注销它的正确地方,例如,电池寿命得到优化。

一对夫妇的事情要考虑前进:

  • 对于一个强大的应用程序,请务必注意,你从onStatusChangedonProviderEnabledonProviderDisabled获得的信息。我已经看到了stackoverflow.com上的各种问题,其答案是关注状态变化等。
  • 此外,为了更好地估计位置,您可以考虑使用Location.accuracy值,因为GPS位置估计的准确性可以换。一分钟他们可能会非常准确,但随后可见卫星发生变化,准确度可能突然降低。在我的应用程序中,我使用了一个简单的卡尔曼滤波器,我在回答问题Smooth GPS data时发布了代码。
  • +0

    干杯,非常感谢... –

    +0

    没问题!如果您发现我的答案有用,您甚至可以将其标记为有用和/或作为答案:-)。 – Stochastically