我有點不知所措:在我的主要活動中,我註冊了一個LocationManager並將其連接到一個LocationListener以使用myLocation.getLatitude()等。訪問類
現在我需要使用另一個類的位置方法。
我不能使用這些對象從另一個類,因爲我不能intantianti主要活動。 我不能使用getters傳遞L.Manager或L.Listener,因爲它們再次是非靜態的。
因此,一般來說,我如何訪問我在主要活動中創建的對象? 關於如何更好地組織這個提示?主活動類中的LocationListener類是一個愚蠢的事情嗎?
public class URNavActivity extends Activity
{
public LocationManager mlocManager;
public LocationListener mlocListener;
...
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
actVar=this;
initGraph();
setMap();
gpsEnable();
initMyLocation();
getItems();
initOverlay();
}
public void gpsEnable()
{
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
myMap.getController().setCenter(new GeoPoint(lati, longi));
}
我會非常感興趣的聽到其他方面的... –
首先很多其他的答案這裏在stackoverflow,什麼工程!我錯過了'在我的代碼中實現LocationListener'。我在服務中創建了一個監聽器,並且遇到了阻止這個問題的問題。 PS:你可以使用'onDestroy(){locationManager.removeUpdates(this); \t locationManager = null; \t stopSelf(); \t super.onDestroy(); }'停止經理。 – Eugen
非常感謝!但是如何在這裏檢查運行時權限? –