我有MainActivty和MyLocationListener类。
MyLocationListener扩展AsyncTask实现LocationListener ..
以下是位置监听器类的代码:
私有类MyLocationListener extends AsyncTask实现了LocationListener {
@Override
public void onLocationChanged(Location location) {
// convert coords from double to string
String lat = Double.toString(location.getLatitude());
String lon = Double.toString(location.getLongitude());
Log.i(AppHelper.APP_LOG_NAMESPACE, "lat " + lat);
Log.i(AppHelper.APP_LOG_NAMESPACE, "lon " + lon);
// save actual position into shared preferences storage
_appPrefs = new AppPreferences(activityContext);
_appPrefs.saveSomeString("lat", lat);
_appPrefs.saveSomeString("lon", lon);
getAddressByCoords(location.getLatitude(),
location.getLongitude());
setPositionToView(activityContext, mView);
}
/**
* Method get string representation of the place in given coords
*
* @param lat
* double
* @param lon
* double
* @return List <Address>
* @throws Exception
*/
public List<Address> getAddressByCoords(double lat, double lon) {
Geocoder gCoder = new Geocoder(activityContext);
try {
addresses = gCoder.getFromLocation(lat, lon, 1);
if (addresses != null && addresses.size() > 0) {
Log.d("APP",
"LOCATION " + addresses.get(0).getAddressLine(0));
Log.d("APP",
"LOCATION " + addresses.get(0).getAddressLine(1));
Log.d("APP",
"LOCATION " + addresses.get(0).getAddressLine(2));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return addresses;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(MainActivity.this,
provider + "'s status changed to " + status + "!",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(MainActivity.this,
"Provider " + provider + " enabled!", Toast.LENGTH_SHORT)
.show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity.this,
"Provider " + provider + " disabled!", Toast.LENGTH_SHORT)
.show();
}
@Override
protected Object doInBackground(Object... params) {
try {
// Define the criteria how to select the location provider
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE); // default
criteria.setCostAllowed(false);
// get the best provider depending on the criteria
provider = locationManager.getBestProvider(criteria, false);
// the last known location of this provider
Location location = locationManager
.getLastKnownLocation(provider);
// request single update
this.onLocationChanged(location);
} catch (Exception e) {
Log.e(AppHelper.APP_LOG_NAMESPACE,
"doInBackground method cannot be processed", e);
e.printStackTrace();
}
return null;
}
}
如果我试图使用以下方法获取GPS坐标:
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mView = view;
boolean isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
// get values from user settings
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(this);
Boolean prefferNativeGPs = sharedPrefs.getBoolean(
"prefNativeGps", false);
Log.i("APP", "GPS enabled " + isGPSEnabled); // false
Log.i("APP", "NETWORK enabled " + isNetworkEnabled); // true
Log.i("APP", "USE native " + prefferNativeGPs); // true
// blink textview
TextView stateTv = (TextView) findViewById(R.id.state);
stateTv.setTextColor(getResources().getColor(R.color.black));
startBlinkText();
Log.i("APP", "GPS POSITION USING GPS_PROVIDER");
Toast.makeText(this,R.string.parking_car_using_gps_it_can_take_more_time,
Toast.LENGTH_LONG).show();
// calling doInBackground
new MyLocationListener().execute("");
我在doInBackground中遇到以下异常:
无法在未调用looper.prepare的线程内创建处理程序
我想问一下,如何修改我的代码来正确处理?
非常感谢您的帮助。
答案 0 :(得分:0)
在AsyncTask的构造函数中创建处理程序。您必须在具有活动消息循环的线程上创建它,例如UI线程。
答案 1 :(得分:0)
获取位置已经是异步操作...
您需要做的就是在UI线程上调用它,并且很快就会得到新的结果(取决于许多事情)。
这就是为什么它有一个听众。否则它只会在阻止你时给你回复...