如何获取GPS位置使用AsyncTask的? [英] How to Get GPS Location Using AsyncTask?
问题描述
我见过很多答案,这种类型的问题,但它不是我的任务有关。我想在后台得到GPS的位置,但我得到的例外,因为斜面创建处理程序内螺纹在Android中
尚未调用尺蠖prepare在 mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mlocListener);
I have seen many answers for this type of question but its not related with my task. I am trying to get gps location in background but i got exception as Cant Create Handler Inside Thread That Has Not Called Looper Prepare in Android
in mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
.
public class GPSLocation extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = new ProgressDialog(RoadMaintenanceActivity.this);
progressDialog.setCancelable(true);
progressDialog.setMessage("Getting GPS Location...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setProgress(1);
progressDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}
@Override
protected void onPostExecute(Void result)
{
progressDialog.cancel();
}
@Override
protected Void doInBackground(Void... params) {
boolean isGps = false;
while(!isGps)
{
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
if(longitude !=0 && latitude!=0)
{
isGps = true;
sendSMS();
}
}
return null;
}
}
我不知道为什么我们不能调用的doBackground()方法中。
I am not sure why we cant call that inside the doBackground() method.
感谢您的帮助球员。
推荐答案
最后我想通了这个问题,我认为这将帮助一些人喜欢我。
Finally i figured out the problem, I think this will help some one like me
public class GPSLocation extends AsyncTask<Void, Void, Void>
{
boolean running =true;
@Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = new ProgressDialog(RoadMaintenanceActivity.this);
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
getgps.cancel(true);
}
});
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
progressDialog.setCancelable(true);
progressDialog.setMessage("Getting GPS Location...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setProgress(1);
progressDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}
@Override
protected void onPostExecute(Void result)
{
progressDialog.cancel();
}
@Override
protected Void doInBackground(Void... params) {
boolean isDataSubmitted = false;
while(!isDataSubmitted)
{
if(longitude !=0 && latitude!=0)
{
sendSMS();
isDataSubmitted = true;
}
}
return null;
}
}
通过在具有的LocationManager上preExecute()
除外从我的应用程序摆脱出来。我们可以得到GPS在preexecute而非 doinbackground()
。
By having Locationmanager in onPreExecute()
the exception get rid out from my application. We can get the gps in onpreexecute rather than doinbackground()
.
这篇关于如何获取GPS位置使用AsyncTask的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!