如何在线程中调用requestLocationUpdates?

问题描述:

我想从一个线程获取我的位置。 我代码:如何在线程中调用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更新的位置? 你能帮我吗?

您拥有开发者文档中的所有内容: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