gpt4 book ai didi

android - 如何使用 AsyncTask 获取 GPS 位置?

转载 作者:行者123 更新时间:2023-11-29 14:28:28 25 4
gpt4 key购买 nike

我已经看到很多此类问题的答案,但它与我的任务无关。我试图在后台获取 gps 位置,但出现异常,因为 无法在未调用 Looper Prepare 的线程内创建处理程序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() 方法中调用它。

感谢您的帮助。

最佳答案

最后我想通了问题,我想这对像我这样的人有帮助

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;
}
}

通过在 onPreExecute() 中使用 Locationmanager,异常从我的应用程序中移除。我们可以在 onpreexecute 而不是 doinbackground() 中获取 gps。

关于android - 如何使用 AsyncTask 获取 GPS 位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11035231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com