我正在创建一个定期更新位置的服务。但使用下面的代码,它抛出了我:
runException 无法在未调用 Looper.prepare() 的线程内创建处理程序
此代码在服务中 Async 类的 doInBackground 内运行
private void UpdatePosition()
{
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location =
locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
showPosition(location);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
showPosition(location);
}
public void onProviderDisabled(String provider){
}
public void onProviderEnabled(String provider){
}
public void onStatusChanged(String provider, int status, Bundle extras){
Log.i("LocAndroid", "Provider Status: " + status);
}
};
locManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 15000, 0, locationListener);
}
我正在查看几个帖子,发现请求必须在 uithread 中运行,但不知道如何或在哪里放置它。
我能做些什么?
编辑:
我只会将此添加到我的代码中,现在不会抛出任何错误
private void UpdatePosition()
{
Looper.prepare();
looper = Looper.myLooper();
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location =
locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
showPosition(location);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
showPosition(location);
}
public void onProviderDisabled(String provider){
}
public void onProviderEnabled(String provider){
}
public void onStatusChanged(String provider, int status, Bundle extras){
Log.i("LocAndroid", "Provider Status: " + status);
}
};
locManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 15000, 0, locationListener);
}
但我不知道它是否正确..你说什么?