onActivityResult result code is zero also when the

2019-08-28 21:35发布

问题:

GOAL: when the user opens the app and the GPS is off a dialog has to show and ask him to turn GPS on.

PROBLEM: the result code passed to onActivityResult hasn't a consistent value. I mean: when I run the app and I click ok quickly on the alert dialog, resultCode is 0 (like if I'd clicked cancel) when I'm in debugging mode, I step through the code slower and I click ok on the alert dialog, resultCode become -1 (as expected)

QUESTION: can you help me to find what's the root of this problem within the code? How can I see exactly where resultCode is assigned a value?

MapsActivity

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {

...

@Override
    public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;


        // Check if localization permission is granted
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

            mMap.setMyLocationEnabled(true);

            // Create location updates request
            mLocationRequest = locationUpdate.createLocationRequest(mLocationRequest, INTERVAL_DURATION);

            // Create location updates callback
            mLocationCallback = locationUpdate.createLocationCallback(mLocationCallback,RSSIcolorHmap,db, getApplicationContext());

            // If wifi is the active connection, enable settings to get location updates and start wifi signal scan
            if(checkWifiConnection()){

                ACTIVE_TECHNOLOGY = 3;

                TextView connTypeTextView = findViewById(R.id.typetextView);
                connTypeTextView.setText("WIFI");

                // AsyncTask that loads Wifi signal area info on the map
                new PopulateWifiAreaAsync(db, getApplicationContext(), mMap, RSSIcolorHmap).execute();

                // Check if location settings are appropriated for the location request
                locationUpdate.checkLocationSettings(mLocationRequest,REQUEST_CHECK_SETTINGS, mFusedLocationClient,mLocationCallback,mMap);

                scanWifiSignal();

            } else {
                // TODO Another connectivity is being used, enable settings to get location updates, check which type of connectivity is and start appropriate signal scan


                Toast.makeText(getApplicationContext(),"no wifi, esci", Toast.LENGTH_LONG).show();
                //finish();
                // Check if location settings are appropriated for the location request
                locationUpdate.checkLocationSettings(mLocationRequest,REQUEST_CHECK_SETTINGS,mFusedLocationClient,mLocationCallback,mMap);
                //Toast.makeText(getApplicationContext(),"no wifi, check tipo connessione attuale", Toast.LENGTH_LONG).show();

            }


        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_FINE_LOCATION);
            }
        }
    }

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        getParent();
        // Check which request we're responding to
        switch (requestCode){
            case REQUEST_CHECK_SETTINGS:
                switch (resultCode){
                    case MapsActivity.RESULT_OK:
                        // All required changes were successfully made.

                         locationUpdate.getLastKnownLocation(mFusedLocationClient);
                         locationUpdate.startLocationUpdates(mFusedLocationClient, mLocationCallback, mLocationRequest);
                        //setActiveConnectionTypeScan();
                        //Toast.makeText(getApplicationContext(), "at", Toast.LENGTH_LONG).show();

                        break;
                    case MapsActivity.RESULT_CANCELED:
                        Toast.makeText(getApplicationContext(), "This app requires location permission to be granted", Toast.LENGTH_LONG).show();

                        finish();
                        break;
                    default:
                        break;
                }
                break;

        }

    }

LocationUpdate

public void checkLocationSettings(final LocationRequest mLocationRequest, final int REQUEST_CHECK_SETTINGS,  final FusedLocationProviderClient mFusedLocationClient, final LocationCallback mLocationCallback, final GoogleMap mMap) {

        //final Activity mapsActivity = (Activity) context;

        // Get and check location services settings
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(mLocationRequest)
                .setAlwaysShow(true);


        SettingsClient client = LocationServices.getSettingsClient(mapsActivity);
        Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());

        task.addOnSuccessListener(mapsActivity, new OnSuccessListener<LocationSettingsResponse>() {
            @Override
            public void onSuccess(LocationSettingsResponse locationSettingsResponse) {

                // All location settings are satisfied. The client can initialize
                // location requests here
                LocationUpdate locationUpdate = new LocationUpdate(mapsActivity);

                locationUpdate.getLastKnownLocation(mFusedLocationClient);
                locationUpdate.startLocationUpdates(mFusedLocationClient, mLocationCallback, mLocationRequest);

                // Determine which type of connectivity is being used
                // setActiveConnectionTypeScan();

                // Toast.makeText(getApplicationContext(),"setting ok", Toast.LENGTH_LONG).show();

            }
        });

        task.addOnFailureListener(mapsActivity, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                if (e instanceof ResolvableApiException) {
                    // Location settings are not satisfied, but this can be fixed
                    // by showing the user a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        ResolvableApiException resolvable = (ResolvableApiException) e;
                        resolvable.startResolutionForResult(mapsActivity,
                                REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException sendEx) {
                        // Ignore the error.
                    }
                }
            }
        });


    }