Android Handle Location Permission Deny (Don't Ask Again)

From Android Marshmallow On-words Building Location Based Application , App should grant Location permission , In the previous tutorial working with FusedLocation API initially we have request for location permission which is need for fetching device location details to get user lastLocation . In this tutorial we will see the use case of how to handle location permission deny with don't ask again i.e checking/knowing the status of don't ask again action by user and handling.

When you make request for permission there will be 3 actions user's can perform .

Action 1 :  Allow 
Action 2 :  Just Deny ( deny without selecting the check-box "don't ask again")
Action 3 :  Force Deny ( deny with selecting the check-box "don't ask again")

When you deny permission by selecting the check-box "don't ask again" After that when you make subsequent request again for the same permission Nothing will happen ( i.e The system will not show "Allow Deny" popup again )  .

  UseCase  
Scenerio
1
Request Permission > Pop-up > allow > (TextView Updated With Permission Granted).

 2
Request Permission > Pop-up > deny > (TextView Updated With Permission Denied).

 3

    Request Permission > Pop-up > deny with "don't ask again" selected >
    (TextView Updated With Permission Denied and Show Button for visit settings).
 
   Now on Click of "visit settings" you will navigate to app settings 
   once you grant permission and resume back to app  inside onResume
   again we will check if permission update the UI  (i.e TextView Updated 
   with Permission  Granted and hide Button for visit settings ).


Use Case 1 ( Permission Granted )


Use Case 2 ( Permission Denied )



Use Case 3 ( Pemission Deny By Selecting Don't Ask Again )


1. AndroidManifest


file : AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tutorialsbuzz.handlelocationpermissiondeny">

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    ...........
	...........
	...........

</manifest>


2. XML Layout


file : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="22sp" />

    <Button
        android:id="@+id/requestBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="Request Permission" />


    <Button
        android:id="@+id/visitAppSettingsBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="Visit App Settings And Allow Permission"
        android:visibility="gone" />

</LinearLayout>


3. Activity


MainActivity Full Code Handling Above 3 Use Cases 

file : MainActivity.kt
package com.tutorialsbuzz.handlelocationpermissiondeny

class MainActivity : AppCompatActivity() {

    val PERMISSION_ID = 42

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        requestBtn.setOnClickListener({
            if (!checkPermissionsGranted())
                requestPermissions();
        })

        visitAppSettingsBtn.setOnClickListener({
            val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
            val uri = Uri.fromParts("package", packageName, null)
            intent.data = uri
            startActivity(intent)
        })

    }

    override fun onResume() {
        super.onResume()
        if (checkPermissionsGranted()) {
            visitAppSettingsBtn.visibility = View.GONE
            requestBtn.visibility = View.GONE
            label.setText("Permission Granted")
        } else {
            label.setText("Permission Denied")
            checkUserRequestedDontAskAgain()
        }

    }

    fun requestPermissions() {
        ActivityCompat.requestPermissions(
            this,
            arrayOf(
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION
            ),
            PERMISSION_ID
        )
    }

    fun checkPermissionsGranted(): Boolean {
        return ActivityCompat.checkSelfPermission(
            this, Manifest.permission.ACCESS_FINE_LOCATION
        ) == PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(
                    this, Manifest.permission.ACCESS_COARSE_LOCATION
                ) == PackageManager.PERMISSION_GRANTED
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        if (requestCode == PERMISSION_ID) {
            if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                //when granted
                label.setText("Permission Granted:")
                showToast("Permission Granted:")
            } else {
                //Permission Denied
                label.setText("Permission Denied:")
                showToast("Permission Denied:")

                checkUserRequestedDontAskAgain()
            }
        }
    }

    private fun checkUserRequestedDontAskAgain() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val rationalFalgCOARSE =
                shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION)
            val rationalFalgFINE =
                shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)
            if (!rationalFalgCOARSE && !rationalFalgFINE) {
                label.setText("permission_denied_forcefully")
                visitAppSettingsBtn.visibility = View.VISIBLE
                requestBtn.visibility = View.GONE
                showToast("permission_denied_forcefully")
            }
        }
    }

    fun showToast(msg: String) {
        Toast.makeText(this@MainActivity, msg, Toast.LENGTH_LONG).show()
    }

}


No comments:

Post a comment