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

标签 android android-asynctask

我已经看到很多此类问题的答案,但它与我的任务无关。我试图在后台获取 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/

相关文章:

android - Android 调用电话后按键

java - android: TimeZone.getDefault() 返回 EST,而不是 EDT

java - 在运行时更改菜单

android - 将变量值从 BaseExpandableListAdapter 类转移到新 Activity

java - 进度对话框异步任务参数字符串[]

android - NetworkOnMainThreadException

Android - 按下后退按钮时停止 AsyncTask 并返回到上一个 Activity

android - 错误 :(49) undefined reference to 'cv::Stitcher::createDefault(bool)' in using OpenCV native in Android

java - 如何将列表或数组列表从 AsyncTask 类传递到另一个类

java - 在异步线程中解析时出现 fatal error (AsyncTask.doInBackground())