Android Marshmallow Request Runtime Permission Android Studio example tutorial

How to manually ask about access permission at application run time via User.

Android marshmallow 6.0 comes with an amazing functionality which is called as ” Request Runtime Permission “. This feature will gives even more privacy control to android application users because some of app permissions which is dangerous for example location access service permission and access mobile phone contacts permission and there are lot’s of permissions. You can read about them Here . With this type of method app users can manually allow or disallow permission on their applications. So here is the complete step by step tutorial for Android Marshmallow Request Runtime Permission Android Studio example tutorial.

android-project-download-code-button

Import Note :  Please add below access contact permission inside your AndroidManifest.xml file because only android Marshmallow + above android OS support Run Time permission and below version from Marshmallow it will automatically access that permission.

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

Android Marshmallow Request Runtime Permission Android Studio example tutorial.

Code for MainActivity.java file.

package com.android_examples.runtimepermission_android_examplescom;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button button1Enable, buttonChk ;
    public  static final int RequestPermissionCode  = 1 ;
    boolean RequestTF ;
    int RequestCheckResult  ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1Enable = (Button)findViewById(R.id.button);

        buttonChk = (Button)findViewById(R.id.button2);

        button1Enable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                EnableRuntimePermission();

            }
        });

        buttonChk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PermissionStatus() ;

                if (RequestTF == true) {

                    Toast.makeText(MainActivity.this," Permission Enable ",Toast.LENGTH_LONG).show();

                } else {

                    Toast.makeText(MainActivity.this," Permission Not Enable ",Toast.LENGTH_LONG).show();

                }

            }
        });
    }

    public void EnableRuntimePermission(){

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                Manifest.permission.READ_CONTACTS))
        {

            Toast.makeText(MainActivity.this,"CONTACTS permission allows us to Access CONTACTS app", Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(MainActivity.this,new String[]{
                    Manifest.permission.READ_CONTACTS}, RequestPermissionCode);

        }
    }

    @Override
    public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {

        switch (RC) {

            case RequestPermissionCode:

                if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED) {

                    Toast.makeText(MainActivity.this,"Permission Granted, Now your application can access CONTACTS.", Toast.LENGTH_LONG).show();

                } else {

                    Toast.makeText(MainActivity.this,"Permission Canceled, Now your application cannot access CONTACTS.", Toast.LENGTH_LONG).show();

                }
                break;
        }
    }

    public void PermissionStatus(){

        RequestCheckResult = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_CONTACTS);

        if (RequestCheckResult == PackageManager.PERMISSION_GRANTED){

            RequestTF = true;

        } else {

            RequestTF = false;

        }

    }

}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.android_examples.runtimepermission_android_examplescom.MainActivity">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="REQUEST TO ENABLE RUNTIME PERMISSION"
        android:id="@+id/button"
        android:layout_above="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Check Permission is enable or Not"
        android:id="@+id/button2"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

Code for AndroidManifest.xml file.

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Screenshots:

Marshmallow-permission-1

Android Marshmallow Request Runtime Permission Android Studio example tutorial

Marshmallow-permission-3

Click here to download Android Marshmallow Request Runtime Permission Android Studio example tutorial project with source code.

2 Comments

  1. How to disable Deny button in Android Runtime Permission.

Leave a Reply to Purushotham Cancel reply

Your email address will not be published. Required fields are marked *