我正在嘗試創建一個安卓服務,每隔5分鐘就會更新用戶的位置信息。我正在使用DDMS將座標發送到可以正常工作的模擬器。我需要轉換這些座標,並獲得位置例如:紐約和使用吐司,在屏幕上打印它。我沒有使用任何地圖,我嘗試使用Geocoder將DDMS提供的座標轉換爲位置。我沒有得到錯誤,但似乎座標沒有被轉換到位置,並且屏幕上沒有顯示任何內容。請幫忙一直在爲此奮鬥很長一段時間。這是我的代碼。由於用於更新用戶位置的Android服務
public class GetLocationService extends Service {
protected LocationManager locationManager;
Button start;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
LocationListener ll = new MyLocListener();
Location location = new Location("abc");
ll.onLocationChanged(location);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, ll);
return START_STICKY;
}
private class MyLocListener implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
}
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Toast.makeText(getApplicationContext(),
location.getLatitude() + "" + location.getLongitude(),
Toast.LENGTH_LONG).show();
try{
Geocoder geo = new Geocoder(GetLocationService.this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Log.d("TAG",addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +"," + addresses.get(0).getAdminArea() + "," + addresses.get(0).getCountryName());
}
Toast.makeText(getApplicationContext(),
addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +"," + addresses.get(0).getAdminArea() + "," + addresses.get(0).getCountryName(),
Toast.LENGTH_LONG).show();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
////啓動服務////
public class MyService extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.service);
startService(new Intent(getBaseContext(), GetLocationService.class));
}
}
您的日誌語句是否正在執行?此外,是否將Toast.makeText()中的「GetLocationService.this」更改爲「getApplicationContext()」,因爲可能您的麪包沒有發送到正確的位置。 – javajavajava 2012-07-10 18:04:12
@ user1454749請檢查我的編輯。我試圖將其更改爲「GetLocationService.this」 – 2012-07-10 18:26:58
您是否知道100%服務正在啓動?你確定OnStartCommand()方法被調用嗎? – javajavajava 2012-07-10 18:38:01