将Android应用程序中的GPS功能外包给一个单独的类

| 我遵循了http://developer.android.com/guide/topics/location/obtaining-user-location.html,当处于onCreate方法中的活动中时,此方法工作正常。 然后,我想在一个单独的名为LocationHelper的类中将此功能外包。
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class LocationHelper {

public Context mContext;
public Location loc;

public LocationHelper (Context mContext){
    this.mContext = mContext;

    // Acquire a reference to the system Location Manager
    LocationManager locationManager = (LocationManager)     mContext.getSystemService(Context.LOCATION_SERVICE);

    // Define a listener that responds to location updates
    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
          // Called when a new location is found by the network location provider.
            setLocation(location);
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
      };

    // Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}

public void setLocation(Location location) {
    this.loc = location;
}

public Location getLocation() {
    return this.loc;
}
}
在活动中,我这样做;基本上,我想从助手类中提取GPS坐标(用于测试!)并显示它。问题是,位置始终为空。
public class GraffitiWall extends Activity {

private TextView tv;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tv = new TextView(this);

    LocationHelper gpsie = new LocationHelper(this);
    while (true){
        makeUseOfNewLocation(gpsie.getLocation());
    }
}

public void makeUseOfNewLocation(Location loc){
    if (loc == null){return;}
    tv.setText(\"\" + loc.getLatitude());
    setContentView(tv);
}
}
我想念什么并且做错了什么?     
已邀请:
在您的onCreate方法中放入无限循环是一个坏主意。您的问题很可能是由于onCreate无法完成并将控制权传递回OS而引起的。如果这引起强制关闭错误,我不会感到惊讶。 也许您需要做的就是创建一个服务,该服务将对您的位置进行监控并从那里更新您的活动。     

要回复问题请先登录注册