我对 doInBackground() 中的 looper.prepare() 有疑问。如果我不使用 looper.prepare,它会生成一个异常“无法在未调用 Looper.prepare() 的线程内创建处理程序”,当我使用 looper.prepare 时,它对 looper.prepare 的 5 次调用和第六次调用都可以正常工作它产生以下异常。
08-15 11:29:48.078: W/System.err(14133): java.lang.RuntimeException: Only one Looper may be created per thread
08-15 11:29:48.088: W/System.err(14133): at android.os.Looper.prepare(Looper.java:74)
08-15 11:29:48.088: W/System.err(14133): at the.bike.washer.SelectLocation$setUserCurrentLocation.doInBackground(SelectLocation.java:1504)
08-15 11:29:48.088: W/System.err(14133): at the.bike.washer.SelectLocation$setUserCurrentLocation.doInBackground(SelectLocation.java:1)
08-15 11:29:48.088: W/System.err(14133): at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-15 11:29:48.088: W/System.err(14133): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-15 11:29:48.088: W/System.err(14133): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-15 11:29:48.088: W/System.err(14133): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-15 11:29:48.088: W/System.err(14133): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-15 11:29:48.088: W/System.err(14133): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-15 11:29:48.088: W/System.err(14133): at java.lang.Thread.run(Thread.java:856)
这是代码
try {
Looper.prepare();
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
double lat = 48.8742;
double lng = 2.3470;
Constants.coordinates = new LatLng(lat, lng);
Constants.address = getFromLocation(lat, lng, 1);
result = "no gps";
} else {
//this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, SelectLocation.this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Constants.coordinates = new LatLng(latitude, longitude);
Constants.address = getFromLocation(latitude, longitude, 1);
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, SelectLocation.this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Constants.coordinates = new LatLng(latitude, longitude);
Constants.address = getFromLocation(latitude, longitude, 1);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}