N
N
Nikita2018-07-17 19:40:02
Java
Nikita, 2018-07-17 19:40:02

How to find out the location from the profile settings?

you need to find out the location before launching the activity and load the content based on it,
my code does not work correctly, because the location flag does not work

Boolean locationFlag = false;  //переменная которая должна стать true от локации
//пишу все в onCreate, пробовал в onResume менять флаг, не работает 
        mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
          MY_PERMISSION_ACCESS_COURSE_LOCATION);
    }

    mFusedLocationProviderClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>(){
      @Override
      public void onSuccess(Location location) {
        if (location != null) {
        Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault());
        try {
          List<Address> addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
          if (addresses.size() > 0) {
            String locationCountry = addresses.get(0).getCountryCode().toLowerCase();
            Log.d("TAG", "AAAAA"+LINK);
            if("ua".equals(locationCountry) || "ru".equals(locationCountry)){
            locationFlag = true;
            LINK += locationCountry;
            Log.d("TAG", "BBBB "+LINK);
            }
          }
        } catch (IOException e) {
          e.printStackTrace();
        }
      }}
    });

    setContentView(R.layout.activity_start);
        webView = findViewById(R.id.webView);
        webView.setWebViewClient(new MyWebViewClient());

        if(locationFlag){
      webView.getSettings().setJavaScriptEnabled(true);
      new MyWebViewClient().shouldOverrideUrlLoading(webView, LINK);
      Log.d("TAG", "after loading   "+LINK);
    }else{
            Intent intent = new Intent();
            intent.setClass(this, MainActivity.class);
            startActivity(intent);
        }
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question