0

早上好,我尝试实现先请求权限然后执行某些操作但没有成功的方法...

这是我的 MainActivity 代码:

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();
                    }
                });
       }
}

问题是执行函数 ApiMarkers.getMarkers(.... 纬度和经度填充...) 但由于纬度和经度未加载而无法执行...我花了两个晚上没有解决方案:(感谢您的帮助!克里斯蒂安

4

0 回答 0