2012-07-10 35 views
0

我正在嘗試創建一個安卓服務,每隔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)); 

    } 
} 

enter image description here

+0

您的日誌語句是否正在執行?此外,是否將Toast.makeText()中的「GetLocationService.this」更改爲「getApplicationContext()」,因爲可能您的麪包沒有發送到正確的位置。 – javajavajava 2012-07-10 18:04:12

+0

@ user1454749請檢查我的編輯。我試圖將其更改爲「GetLocationService.this」 – 2012-07-10 18:26:58

+0

您是否知道100%服務正在啓動?你確定OnStartCommand()方法被調用嗎? – javajavajava 2012-07-10 18:38:01

回答

0

好了,所以都來通過你的日誌報表?另外,你的toast不會顯示,因爲你傳遞的上下文是服務上下文。服務不是用戶界面的一部分,因此服務上下文沒有被顯示。應用程序上下文可能工作......但您確實應該使用當前活動的上下文。

+0

否日誌語句也不會通過。我已經上傳了一個日誌貓的圖片。 – 2012-07-10 18:27:37

+0

Geocoder類需要一個未包含在覈心android框架中的後端服務。你有嗎? – Joel 2012-07-10 18:30:11

+0

你確定嗎?因爲我曾經嘗試過使用Geocoder類,它在沒有任何後端服務的情況下爲我工作。我試着在上面給出的代碼中使用它,它不起作用。 – 2012-07-10 18:33:52