VladMorzhanov
9/23/2017 - 2:42 PM

Android on activity result when requested enable bluetooth or gps.

Android on activity result when requested enable bluetooth or gps.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        // User chose not to enable Bluetooth.
        if (requestCode == REQUEST_ENABLE_BT) {
            if (resultCode == Activity.RESULT_CANCELED) {
                layoutEnableServices.setVisibility(View.VISIBLE);
                layoutScan.setVisibility(View.INVISIBLE);
                tvEnableServices.setText(R.string.message_bluetooth_enable);
                btnEnableService.setText(R.string.enable_bluetooth);
                userCanceledServiceEnabling = true;
                return;
            } else
                userCanceledServiceEnabling = false;
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                        REQUEST_PERMISSION_GPS_CODE);
            } else {
                layoutEnableServices.setVisibility(View.INVISIBLE);
                layoutScan.setVisibility(View.VISIBLE);
            }
        }

        if (requestCode == REQUEST_PERMISSION_GPS_CODE) {
            if (resultCode == Activity.RESULT_CANCELED) {
                layoutEnableServices.setVisibility(View.VISIBLE);
                layoutScan.setVisibility(View.INVISIBLE);
                tvEnableServices.setText(R.string.pls_enable_gps);
                btnEnableService.setText(R.string.enable_gps);
                return;
            } else {
                Intent enableLocationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(enableLocationIntent, REQUEST_SERVICE_GPS_CODE);
            }
        }

        if (requestCode == REQUEST_SERVICE_GPS_CODE) {
            if (resultCode == Activity.RESULT_CANCELED) {
                layoutEnableServices.setVisibility(View.VISIBLE);
                layoutScan.setVisibility(View.INVISIBLE);
                tvEnableServices.setText(R.string.pls_enable_gps);
                btnEnableService.setText(R.string.enable_gps);
                return;
            } else {
                layoutEnableServices.setVisibility(View.INVISIBLE);
                layoutScan.setVisibility(View.VISIBLE);
            }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }