在主要活动中启动了位置侦听器,并希望与作为主要扩展的其他类共享位置更新。尝试了几种不同的变化,但似乎没有任何工作可以完成。 Toast消息显示Intent中的每次更改,并且在Eclipse中更新GPS时,只需要数据。寻找一点帮助或不同的观点。在Android中拥有有限的专业知识,但相信这可以在主要活动中完成而不是单独的Thread或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;
}