我已经实现了一个简单的 GPS 程序,它获取坐标并将它们显示在屏幕上。当我尝试改进此设计并实现异步任务时,GPS 似乎由于某种原因无法工作。将异步任务与 GPS 一起使用是否存在一些问题?这是我的代码:
private class DownloadTask extends AsyncTask<String, Void, Object> {
protected Object doInBackground(String... args) {
Log.i("MyApp", "Background thread starting");
LocationManager mLocationManager;
Gpslistener Gpslistener;
Gpslistener = new Gpslistener();
try{
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,Gpslistener);
}catch (Exception e) {
test_gps = true;
}
return null;
}
protected void onPostExecute(Object result) {
if(test_gps == true){
if (ParkingActivity.this.progressDialog != null) {
ParkingActivity.this.progressDialog.dismiss();
}
AlertMessage("GPS Error", "Unable to get location");
}
public class Gpslistener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
}
@Override
public void onProviderDisabled(String provider)
{
}
@Override
public void onProviderEnabled(String provider)
{
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
每次我运行它时,它总是会捕获一个异常。GPS 权限在清单中设置,我总是使用不同的应用程序来确保 GPS 连接在线,因此我将这些作为潜在错误排除了。老实说,我想不出其他任何事情,没有异步任务它可以完美运行!非常感谢任何帮助,谢谢!
编辑 了我的异常 logcat:
05-09 22:56:20.199: E/EXCEPTION:(8874): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-09 22:56:20.199: E/EXCEPTION:(8874): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-09 22:56:20.199: E/EXCEPTION:(8874): at android.os.Handler.<init>(Handler.java:121)
05-09 22:56:20.199: E/EXCEPTION:(8874): at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:173)
05-09 22:56:20.199: E/EXCEPTION:(8874): at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:173)
05-09 22:56:20.199: E/EXCEPTION:(8874): at android.location.LocationManager._requestLocationUpdates(LocationManager.java:579)
05-09 22:56:20.199: E/EXCEPTION:(8874): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446)
05-09 22:56:20.199: E/EXCEPTION:(8874): at stefan.testservice.ParkingActivity$DownloadTask.doInBackground(ParkingActivity.java:163)
05-09 22:56:20.199: E/EXCEPTION:(8874): at stefan.testservice.ParkingActivity$DownloadTask.doInBackground(ParkingActivity.java:1)
05-09 22:56:20.199: E/EXCEPTION:(8874): at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-09 22:56:20.199: E/EXCEPTION:(8874): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
05-09 22:56:20.199: E/EXCEPTION:(8874): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
05-09 22:56:20.199: E/EXCEPTION:(8874): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
05-09 22:56:20.199: E/EXCEPTION:(8874): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
05-09 22:56:20.199: E/EXCEPTION:(8874): at java.lang.Thread.run(Thread.java:1019)