Code Example

Android Marshmallow Permission Snippets

Learn about Marshmallow Permissions implementation in android via simple code snippets and examples.

1. Android Marshmallow Permission Example

A simple step by step Android Runtime Marshmallow Permission example.

Learn how to implement Marshmallow Permissions in this code snippet.

This example will comprise the following files:

  • HomeActivity.java

Step 1: Create Project

  1. Open your AndroidStudio IDE.
  2. Go to File-->New-->Project to create a new project.

Step 2: Dependencies

No special dependency is needed for this project.

Step 3: Design Layouts

*(a). activity_home.xml

Create a file named activity_home.xml and design it as follows:

<?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:id="@+id/activity_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.aashish.marshmallowpermission.HomeActivity">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:src="@drawable/images"
    android:id="@+id/imageview"
    android:scaleType="fitStart"/>

    <Button
        android:layout_width="200dp"
        android:text="Permission"
        android:textColor="#fff"
        android:layout_marginTop="75dp"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:layout_below="@+id/imageview"
        android:background="@color/colorPrimary"
        android:id="@+id/btn_permission"
        android:layout_height="75dp"
        />

</RelativeLayout>

Step 4: Write Code

Write Code as follows:

(a). HomeActivity.java

Create a file named HomeActivity.java

Here is the full code

package com.example.aashish.marshmallowpermission;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
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 HomeActivity extends AppCompatActivity implements View.OnClickListener {

    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        button=(Button)findViewById(R.id.btn_permission);
        button.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch(view.getId())
        {
            case R.id.btn_permission:
                if(ReadMsgAllowed()){
                    //If permission is already having then showing the toast
                    Toast.makeText(HomeActivity.this,"You already have the permission",Toast.LENGTH_LONG).show();
                    //Existing the method with return
                    return;
                }
                requestReadMsgPermission();

        }

    }

    private void requestReadMsgPermission() {
    if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_SMS)){

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

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if(requestCode==200)
        {
            if(grantResults.length>0&& grantResults[0]==PackageManager.PERMISSION_GRANTED)
            {
                Toast.makeText(getApplicationContext(),"Permission Granted",Toast.LENGTH_LONG).show();
            }
            else
            {
                Toast.makeText(getApplicationContext(),"Permission Rejected",Toast.LENGTH_LONG).show();
            }
        }

    }

    private boolean ReadMsgAllowed() {
        //Getting the permission status
        int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS);

        //If permission is granted returning true
        if (result == PackageManager.PERMISSION_GRANTED)
            return true;

        //If permission is not granted returning false
        return false;
    }
}

Run

Simply copy the source code into your Android Project,Build and Run.

Read More.