2012-10-16 45 views
0

我想從一個線程獲取我的位置。 我代碼:如何在線程中調用requestLocationUpdates?

LocationManager locationManager; 
      GeoPoint p; 
      locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
      Criteria cri = new Criteria(); 
      String tower = locationManager.getBestProvider(cri, false); 
      Location location = locationManager.getLastKnownLocation(tower);  

但如何調用requestLocationUpdates更新的位置? 你能幫我嗎?

回答

1

您擁有開發者文檔中的所有內容:Requesting Location Updates

// Acquire a reference to the system Location Manager 
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

// Define a listener that responds to location updates 
LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     // Called when a new location is found by the network location provider. 
     makeUseOfNewLocation(location); 
    } 

    public void onStatusChanged(String provider, int status, Bundle extras) {} 

    public void onProviderEnabled(String provider) {} 

    public void onProviderDisabled(String provider) {} 
    }; 

// Register the listener with the Location Manager to receive location updates 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
+0

如果在線程中調用LocationListener locationListener = new LocationListener(),則顯示excel。 – mum

+0

你會得到什麼例外? Post logcat –

+0

java.lang.RuntimeException:無法在未調用Looper.prepare()的線程內創建處理程序() – mum

相關問題