当用户单击“确定”时,onActivityResult结果代码也为零

时间:2018-09-21 10:24:33

标签: java android permissions gps onactivityresult

目标:当用户打开应用并关闭GPS后,必须显示一个对话框,要求他打开GPS。

问题:传递给onActivityResult的结果代码的值不一致。我的意思是:当我运行该应用程序并在警报对话框中快速单击“确定”时,处于调试模式时resultCode为0(就像我单击了“取消”一样),因此我会逐步检查代码,在警报对话框中单击“确定”,resultCode变为-1(如预期)

问题::您可以帮助我在代码中找到此问题的根源吗?我怎样才能确切地知道 resultCode 在何处分配了值?

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


    }

0 个答案:

没有答案