Handle the Permissions in Marshmallow.

class PermissionMainActivityKotlin : AppCompatActivity(), GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener, LocationListener {

    private var location: Location? = null    private var locationTv: TextView? = null    private var googleApiClient: GoogleApiClient? = null    private var locationRequest: LocationRequest? = null    // lists for permissions    private var permissionsToRequest: ArrayList<String>? = null    private val permissionsRejected = ArrayList<String>()
    private val permissions = ArrayList<String>()
    private var testButton: Button? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        locationTv = findViewById(R.id.latitude_textview)
        testButton = findViewById(R.id.testButton)
        testButton!!.setOnClickListener { reqPermision() }        // we add permissions we need to request location of the users        permissions.add(Manifest.permission.ACCESS_FINE_LOCATION)
        permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION)

        permissionsToRequest = permissionsToRequest(permissions)
        // we build google api client        googleApiClient = GoogleApiClient.Builder(this).addApi(LocationServices.API).addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build()
    }

    private fun reqPermision() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (permissionsToRequest!!.size > 0) {
                requestPermissions(permissionsToRequest!!.toTypedArray(), ALL_PERMISSIONS_RESULT)
            }
        }
    }

    private fun permissionsToRequest(wantedPermissions: ArrayList<String>): ArrayList<String> {
        val result = ArrayList<String>()

        for (perm in wantedPermissions) {
            if (!hasPermission(perm)) {
                result.add(perm)
            }
        }

        return result
    }

    private fun hasPermission(permission: String): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED        } else true
    }

    override fun onStart() {
        super.onStart()

        if (googleApiClient != null) {
            googleApiClient!!.connect()
        }
    }

    override fun onResume() {
        super.onResume()

        if (!checkPlayServices()) {
            locationTv!!.text = "You need to install Google Play Services to use the App properly"        }
    }

    override fun onPause() {
        super.onPause()

        // stop location updates        if (googleApiClient != null && googleApiClient!!.isConnected) {
            LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this as LocationListener)
            googleApiClient!!.disconnect()
        }
    }

    private fun checkPlayServices(): Boolean {
        val apiAvailability = GoogleApiAvailability.getInstance()
        val resultCode = apiAvailability.isGooglePlayServicesAvailable(this)

        if (resultCode != ConnectionResult.SUCCESS) {
            if (apiAvailability.isUserResolvableError(resultCode)) {
                apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
            } else {
                finish()
            }

            return false        }

        return true    }

    override fun onConnected(bundle: Bundle?) {
        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION            ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_COARSE_LOCATION            ) != PackageManager.PERMISSION_GRANTED        ) {
            return        }

        // Permissions ok, we get last location        location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient)

        if (location != null) {
            locationTv!!.text = "Latitude : " + location!!.latitude + "\nLongitude : " + location!!.longitude        }

        startLocationUpdates()
    }

    private fun startLocationUpdates() {
        locationRequest = LocationRequest()
        locationRequest!!.priority = LocationRequest.PRIORITY_HIGH_ACCURACY        locationRequest!!.interval = UPDATE_INTERVAL        locationRequest!!.fastestInterval = FASTEST_INTERVAL
        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION            ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_COARSE_LOCATION            ) != PackageManager.PERMISSION_GRANTED        ) {
            Toast.makeText(this, "You need to enable permissions to display location !", Toast.LENGTH_SHORT).show()
        }

        LocationServices.FusedLocationApi.requestLocationUpdates(
            googleApiClient,
            locationRequest,
            this as LocationListener
        )
    }

    override fun onConnectionSuspended(i: Int) {}

    override fun onConnectionFailed(connectionResult: ConnectionResult) {}

    override fun onLocationChanged(location: Location?) {
        if (location != null) {
            locationTv!!.text = "Latitude : " + location.latitude + "\nLongitude : " + location.longitude        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        when (requestCode) {
            ALL_PERMISSIONS_RESULT -> {
                for (perm in permissionsToRequest!!) {
                    if (!hasPermission(perm)) {
                        permissionsRejected.add(perm)
                    }
                }

                if (permissionsRejected.size > 0) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (shouldShowRequestPermissionRationale(permissionsRejected[0])) {
                            AlertDialog.Builder(this)
                                .setMessage("These permissions are mandatory to get your location to get near by location API call")
                                .setPositiveButton("OK") { dialogInterface, i ->                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                        requestPermissions(permissionsRejected.toTypedArray(), ALL_PERMISSIONS_RESULT)
                                    }
                                }.setNegativeButton("Cancel", null).create().show()
                            return                        }
                    }
                } else {
                    if (googleApiClient != null) {
                        googleApiClient!!.connect()
                    }
                }
            }
        }
    }

    companion object {
        private val PLAY_SERVICES_RESOLUTION_REQUEST = 9000        private val UPDATE_INTERVAL: Long = 5000        private val FASTEST_INTERVAL: Long = 5000 // = 5 seconds        // integer for permissions results request        private val ALL_PERMISSIONS_RESULT = 1011    }
}

No comments:

Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...