0

已在主活动中启动了位置侦听器,并希望与作为主活动扩展的其他类共享位置更新。尝试了几种不同的变体,但似乎没有任何工作可以完成。Toast 消息会随着 Intent 的每次更改而显示,当 GPS 在 Eclipse 中更新时,只需要数据。寻求一点帮助或不同的观点。在 Android 方面的专业知识有限,但相信这可以在主要活动中完成,而不是在单独的线程或 AsyncTask 中完成。感谢您的帮助!

这是主要设置的位置管理器。

private void initSystems() {
    // Location Manager
    LocationManager lm;
    String context = Context.LOCATION_SERVICE;
    lm = (LocationManager)getSystemService(context);
    // Set up Location Manager
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    Provider = lm.getBestProvider(criteria, true);
    Location location = lm.getLastKnownLocation(Provider);
    updateWithNewLocation(location);
    lm.requestLocationUpdates(Provider, 100, 10,locListener);
}

这是侦听器和更新

private final LocationListener locListener = new LocationListener() {
    public void onLocationChanged(Location loc) {
        updateWithNewLocation(loc);
       Toast.makeText(getApplicationContext(), "onLocationChanged", Toast.LENGTH_SHORT).show();
    }

    public void onProviderDisabled(String Provider){
        updateWithNewLocation(null);
    }

    public void onProviderEnabled(String Provider){ }
    public void onStatusChanged(String Provider, int status, Bundle extras){
        Toast.makeText(getApplicationContext(), "onStatusChanged", Toast.LENGTH_SHORT).show();  
    }
};

private double updateWithNewLocation(Location loc) {
    double loc_update = 0;
    if (loc != null) {
        lat = loc.getLatitude();
        lng =- loc.getLongitude();
        loc_update = lat+lng;
        Toast.makeText(getApplicationContext(), "Lat & Long" + lat + " "+lng, Toast.LENGTH_LONG).show();
        Log.d(DEBUG_TAG, "Lat & Long " + lat + " "+lng);
        return loc_update;
    }
    else {
        Toast.makeText(getApplicationContext(), "Unable to Obtain GPS Data", Toast.LENGTH_SHORT).show();
        Log.d(DEBUG_TAG, "Unable to Obtain GPS Data");
    }
    return loc_update;
}
4

0 回答 0