对于这种类型的问题,我已经看到了许多答案,但与我的任务无关。我试图在后台获取gps位置,但在Cant Create Handler Inside Thread That Has Not Called Looper Prepare in Android中以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。

08-28 14:13