0

Goodmorning, I've tried to implement the method that request permission first and than execute something but without success ...

Here is my MainActivity code:

public class MainActivity extends AppCompatActivity {

public static String latitudineCorrente = ""; //current Latitude
public static String longitudineCorrente = ""; //current Longitude

private SharedPreferences sharedPreferences; //shared preferences
private SharedPreferences.Editor mEditor; // shared preferences editor

/* Variable for getting location */
private FusedLocationProviderClient fusedLocationProviderClient;
private LocationRequest locationRequest;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String appLanguage = "ita";
        LanguageUtil.setAppLanguage(getApplicationContext(), appLanguage);
        /* .... other code.... */

        sharedPreferences = this.getSharedPreferences("appname", MODE_PRIVATE);

        mEditor = sharedPreferences.edit(); // set edit sharedpreferences

        richiamaPermessi(); //permission request

        requestLocationUpdates(); // function requestlocationupdates();

        CaricamentoInizialeApplicazione(); // function for presetting configurations
    }


    private void CaricamentoInizialeApplicazione() {

        boolean configIniziale = sharedPreferences.getBoolean("configurazioneinizialeok", false);

        if(!configIniziale)
        {
            /* can't execute this because Double.parseDouble(this.latitudineCorrente), Double.parseDouble(this.longitudineCorrente) */
            ApiMarkers.getMarkers(MainActivity.this, 0, 400,
                                  Double.parseDouble(this.latitudineCorrente), Double.parseDouble(this.longitudineCorrente),
                       0, 0,0,1, 0,0,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                          /* response elaborations */

                       }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                            dialog.dismiss();

                            Toast.makeText(MainActivity.this, "" + error.getMessage(), Toast.LENGTH_LONG).show();

                            error.printStackTrace();

                        }
                    });


            mEditor.putBoolean("configurazioneinizialeok", true);
            mEditor.commit();

       }

}

/* Function for requist location updates */
public void requestLocationUpdates() {

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) ==
                PermissionChecker.PERMISSION_GRANTED &&  ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) ==
                PermissionChecker.PERMISSION_GRANTED) {


            fusedLocationProviderClient = new FusedLocationProviderClient(this);
            locationRequest = new LocationRequest();

            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            locationRequest.setFastestInterval(1500);
            locationRequest.setInterval(3000);


            fusedLocationProviderClient.requestLocationUpdates(locationRequest, new LocationCallback() {
                @Override
                public void onLocationResult(LocationResult locationResult) {
                    super.onLocationResult(locationResult);

                    mEditor = sharedPreferences.edit();

                    mEditor.putString("latitudine", String.valueOf(locationResult.getLastLocation().getLatitude()));
                    mEditor.putString("longitudine", String.valueOf(locationResult.getLastLocation().getLongitude()));

                    MainActivity.latitudineCorrente = String.valueOf(locationResult.getLastLocation().getLatitude());
                    MainActivity.longitudineCorrente = String.valueOf(locationResult.getLastLocation().getLongitude());

                    Log.d("coordinate", "MAIN ACTIVITY: Latitudine: " + MainActivity.latitudineCorrente + "/"
                            + "Longitudine: " + MainActivity.longitudineCorrente);
                }



            }, getMainLooper());

        } else richiamaPermessi();
    }

    /* Function for requesting access */ 
    public void richiamaPermessi()
    {
        Permissions.check(this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION},
                "Permessi di locazione obbligatori per determinare la propria posizione!",
                new Permissions.Options().setSettingsDialogTitle("Avviso").setRationaleDialogTitle("Permessi locazione"),
                new PermissionHandler() {
                    @Override
                    public void onGranted() {
                        requestLocationUpdates();
                    }

                    @Override
                    public void onDenied(Context context, ArrayList<String> deniedPermissions) {
                        super.onDenied(context, deniedPermissions);
                        richiamaPermessi();
                    }
                });
       }
}

The problem is executing function ApiMarkers.getMarkers(.... with latitude and longitude filled ...) but can't execute because latitude and longitude are not loaded... I spent two nights without solution :( Thanks for any help! Cristian

  • Anything? Is there an example for creating service location? thanks – Cristian Capannini Jan 19 '20 at 06:25
  • I tried to follow this video on youtube https://www.youtube.com/watch?v=gEcFf2Mv4L0 and in any case I could not understand where or when I can call the method "CaricamentoInizialeApplicazione ()" in order to call a method that uses the location coordinates! What I want is to make sure that I don't call the method already said until the location coordinates are loaded into the variables indicated in the code! Is it possible that someone is not able to solve the problem? Thank you – Cristian Capannini Jan 19 '20 at 10:02

0 Answers0