Android Marshmallow Permissions | Requesting Run-Time Permissions

Introduction

Now a days you can notice permissions popping up in android marshmallow phones (ver 6.0 android) when you deny permission then you might have notice a error when you try to make a call, read a message, access your contacts, camera, and few more services for your app in Android Marshmallow 6.0 (API level 23).

Want to know the reason behind it then this android marshmallow development tutorial is for you.

Android Marshmallow has got some advanced security features so as to make sure that user know what type of android marshmallow permission a app require while user try to use them i.e., when ever you try to open a app for first time it will ask you few permissions depending upon the app functionality which we will cover in this  android marshmallow tutorial.

 

android marshmallow permission

 

 

Earlier versions of android before to lollipop might have not requested you for permission but are included in android marshmallow features yes its a safety feature for controlling user data from being taken away without any user notice as in earlier cases but now are being controlled in android 6.0 phones. 

By now you might got an idea what would it look like so you need to few more things as all permissions are not asked in this manner only few are listed out and which are termed to be dangerous permissions and which are to be notified to user and after getting permission will move further.

This permission will provide the safety for the user information by making sure he know what permissions are required by the app.

 

Want to know more information regarding this topic

Requesting Permissions at Run Time

 

 

I will show you how to make a call using a permission window in android marshmallow in this tutorial.

 

MainActivity.java :

 

Here in this tutorial we need not required to design a screen as we are not performing any user clickable functionality as the app starts up it will ask a permission for making a call through the device.

 

Declare a method call() in which call functionality is implemented.

 

void call() {

        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:123" ));
        startActivity(intent);

    }

 

And in oncreate we will check whether permission is granted or not if not granted we need to ask for permission.

 

void checkCallPermission() {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED) {

            requestCallPermission();

        } else {

            call();
        }
    }

 

And permission is not granted the we need to request for permission as

 

private void requestCallPermission() {


        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE},
                REQUEST_CALL);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CALL) {


            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                call();

            } else {

                Toast.makeText(MainActivity.this,"You need to grant call permission to make a call",Toast.LENGTH_LONG).show();
            }

        }
    }

 

Android Marshmallow Full Code :

Providing the full code for Android Marshmallow permission implementation.

 

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {


    private static final int REQUEST_CALL = 0;

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

        checkCallPermission();

    }


    void call() {

        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:123" ));
        startActivity(intent);

    }



    private void requestCallPermission() {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE},
                REQUEST_CALL);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CALL) {

            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                call();

            } else {

                Toast.makeText(MainActivity.this,"You need to grant call permission to make a call",Toast.LENGTH_LONG).show();
            }

        }
    }

    void checkCallPermission() {
      
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED) {


            requestCallPermission();


        } else {

            call();

        }
    }
}

 

And when you run this code you will be prompted to accept a android marshmallow permission regarding call and will make a call if you provide the permission.

 

AndroidManifest.xml

 

Add call permission in manifest file to ask the user for granting the same later.

 

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

 

If you have any query’s in the android marshmallow permission tutorial let us know in comment section below, like, share this tutorial for more interesting tutorials.

Show Buttons
Hide Buttons
Read previous post:
Android Play Audio file Tutorial

Android play audio :   In this tutorial we will deal with android playing audio file, i.e, sometimes we need...

Close