开发者

Outsource GPS functionality in android app to a separate class

开发者 https://www.devze.com 2023-02-24 09:37 出处:网络
I followed http://developer.android.com/guide/topics/location/obtaining-user-location.html and this worked fine when being in the activity in the onCreate-method.

I followed http://developer.android.com/guide/topics/location/obtaining-user-location.html and this worked fine when being in the activity in the onCreate-method.

Then I wanted to outsource this functionality in a separate class called 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) {
    开发者_Go百科this.loc = location;
}

public Location getLocation() {
    return this.loc;
}
}

In the activity I do this; basically I want to pull (for testing purposes!) the GPS coordinates from my helper class and display it. Problem being, the location always is null.

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);
}
}

What am I missing and doing wrong?


Putting an infinite loop in your onCreate method is a bad idea. Your problem is most likely being caused because onCreate is never completing and passing control back to the OS. I wouldn't be surprised if this caused Force Close errors.

Maybe what you need to do is create a service which will do your location monitoring and update your activity from there.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号