Android Location return null after Enabling GPS in dialog

后端 未结 3 1895
一整个雨季
一整个雨季 2021-02-06 06:55

When I enter a screen, I check for if GPS is turned on, if not, the dialog to enable GPS is shown. When user clicks Yes, onActivityResult -> GPS is turned on and I try to get th

3条回答
  •  佛祖请我去吃肉
    2021-02-06 07:53

    Finally, I find a solution: I create a thread and wait for my location available.

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case REQUEST_LOCATION:
                switch (resultCode){
                    case Activity.RESULT_OK:
                        mHandler = new Handler();
                        runnable = new Runnable() {
                            @Override
                            public void run() {
                                mHandler.postDelayed(this,1000);
                                checkLocationAvailable();
                            }
                        };
                        mHandler.postDelayed(runnable,1000);
                        break;
                    case Activity.RESULT_CANCELED:
                        break;
                }
        }
    }
    

    Create a function to stop the thread when my location available.

    private void checkLocationAvailable(){
      if (mMap.getMyLocation() != null) {
      mHandler.removeCallbacks(runnable);
      // do stuff
      }
    }
    

    It is not a good solution, but hope it will help.

提交回复
热议问题