我通过电子邮件点击按钮发送用户的位置。下面给出了AsyncTask的doInBackground。
@Override
protected Boolean doInBackground(String... arg0) {
// TODO Auto-generated method stub
boolean status = false;
try {
double latitude=0;
double longitude=0;
GPSTracker gps=new GPSTracker(MainActivity.this);
if(gps.canGetLocation()){
latitude=gps.getLatitude();
longitude=gps.getLongitude();
}
String mapUrl= "https://www.google.co.in/maps/@"+latitude+","+longitude+",17z";
GMailSender sender = new GMailSender(
"myemail@gmail.com", "password",
getApplicationContext());
status = sender.sendMail(" Subject ", "\nLocation : "+mapUrl,"myemail@gmail.com",
"anothermail@gmail.com");
} catch (Exception e) {
}
return status;
}
但总是将Location返回为0,0
GPSTracker.java
locationManager = (LocationManager) mContext
.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
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
日志显示
05-16 22:01:30.449: W/System.err(12459): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-16 22:01:30.489: W/System.err(12459): at android.os.Handler.<init>(Handler.java:121)
05-16 22:01:30.489: W/System.err(12459): at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:198)
05-16 22:01:30.489: W/System.err(12459): at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:198)
05-16 22:01:30.499: W/System.err(12459): at android.location.LocationManager._requestLocationUpdates(LocationManager.java:604)
05-16 22:01:30.499: W/System.err(12459): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:471)
05-16 22:01:30.509: W/System.err(12459): at com.package.utils.GPSTracker.getLocation(GPSTracker.java:65)
05-16 22:02:07.309: W/System.err(12459): at com.package.utils.GPSTracker.getLocation(GPSTracker.java:65)
05-16 22:02:07.309: W/System.err(12459): at com.package.utils.GPSTracker.<init>(GPSTracker.java:44)
05-16 22:02:07.309: W/System.err(12459): at com.webnamaste.ulrasoundindia.MainActivity$SendMailTask.doInBackground(MainActivity.java:255)
05-16 22:02:07.319: W/System.err(12459): at com.package.ulrasoundindia.MainActivity$SendMailTask.doInBackground(MainActivity.java:1)
05-16 22:02:07.319: W/System.err(12459): at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-16 22:02:07.319: W/System.err(12459): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-16 22:02:07.319: W/System.err(12459): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-16 22:02:07.319: W/System.err(12459): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-16 22:02:07.339: W/System.err(12459): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-16 22:02:07.339: W/System.err(12459): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-16 22:02:07.349: W/System.err(12459): at java.lang.Thread.run(Thread.java:856)
答案 0 :(得分:0)
doInBackground
的方法AsyncTask
在后台线程上执行,它不是主(UI)线程。在这里你可以有3个解决方案:
onPreExecute
并将结果传递至doInBackground
runOnUiThread
致电doInBackground
调用GPSTracker
,这是一个难看的解决方案。Looper.prepare();
和Looper.loop();
看看here
答案 1 :(得分:0)
你正在尝试在gui app中显示一些数据,在doInBackgrund()中,解决方法是 把Looper.preprare()放在doInBackgrund()的第一行。
protected Boolean doInBackground(String... arg0) {
// TODO Auto-generated method stub
Looper.prepare();//This code
boolean status = false;
try {
double latitude=0;
double longitude=0;
GPSTracker gps=new GPSTracker(MainActivity.this);
if(gps.canGetLocation()){
latitude=gps.getLatitude();
longitude=gps.getLongitude();
}
String mapUrl= "https://www.google.co.in/maps/@"+latitude+","+longitude+",17z";
GMailSender sender = new GMailSender(
"myemail@gmail.com", "password",
getApplicationContext());
status = sender.sendMail(" Subject ", "\nLocation : "+mapUrl,"myemail@gmail.com",
"anothermail@gmail.com");
} catch (Exception e) {
}
return status;
}