开发者

Android Location and Threads

i'm trying to make heavy use of the Location API. I have no problem whatsoever doing a simple Activity that refreshes a TextView with a LocationListener, that's fine.

The thing is, i must put all the Location stuff in something like a Manager class that gets called by the main Activity. So i must place everything inside that class and pass say the location to the Activity. This is the Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.m开发者_如何学运维ain);
    g = new GPSManager(getApplicationContext());
    t = new Thread(g);
    t.start();
    updateWithNewLocation(g.getLocation());
}    
private void updateWithNewLocation(Location location) {
    String latLongString;
    TextView myLocationText;
    myLocationText = (TextView)findViewById(R.id.loca);
    if (location != null) {
       double lat = location.getLatitude();
       double lng = location.getLongitude();
       latLongString = "Lat:" + lat + "\nLong:" + lng;
    } else {
       latLongString = "No location found";
    }
    myLocationText.setText("Your Current Position is:\n" +
    latLongString);
 }    

And this is the "Manager" class:

public GPSManager(Context context) {
    locationManager = (LocationManager)context.getSystemService(service); 
    String service = Context.LOCATION_SERVICE;
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    provider = locationManager.getBestProvider(criteria, true); 
}
public Location getLocation() {
    return this.location;
}
private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        Manager.this.location=location;
    }
    public void onProviderDisabled(String provider){
        locationManager.removeUpdates(this);
    }
    public void onProviderEnabled(String provider){ }
    public void onStatusChanged(String provider, int status,
        Bundle extras){ }
    };

@Override
public void run() {
   Looper.prepare();
   locationManager.requestLocationUpdates(provider, 0, 0,
           locationListener);        
   location = locationManager.getLastKnownLocation(provider);
   Looper.loop();
   Looper.myLooper().quit();
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
    locationManager.removeUpdates(locationListener);
}

The problem i have is that, well, it doesn't works at all! If i put this code in the activity, no thread involved, it works great. But this way, it just says "no location found". I need to make the Manager class always listening to changes, so when i ask for the current location is updated.


Option #1: Delete the "manager" class.

Option #2: Make the "manager" class be a Service.

Option #3: Make the "manager" class be a custom Application.

Option #4: Replace the Thread with a HandlerThread.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜