Sunday , January 16 2022

Android Marshmallow Permissions Example – Android Coding by DMTechnolab

The package The trap.Simplified coding.Running;

Import Android.Clearly;

Import the evening.package manager;

Import Android.Support.Annotation.No no;



Import Android.Support.v7.App.AppCompatActivity;

Import Android.The.Bundle;

Import Android.Support.v7.Opinion.Menu.ActionMenuItemView;

Import Android.Opinion.Opinion;

Import Android.Widget.switch;

Import Android.Widget.Toast;

public class Main activity Is spread AppCompatActivity {{

// our button

Private switch Button design;

// Permission code that will be tested in the onRequestPimsResult method


@ Override

Protected Zero Go ahead()Bundle saveInstanceState) {{

Super.Go ahead()saveInstanceState);


// initial button

Button design = ()switch) findViewById()R.Id.Button design);

// add a click listener

Button design.setOnClickListener()New Opinion.OnClickListener()) {{

@ Override

public Zero on click()Opinion v) {{

// first checking if the app is already allowing


// showing toast if permission is already there

Toast.Makeup()Main activity.this,“You already have permission”,Toast.LENGTH_LONG).Display());

// Returns the existing method with

The return;


// Ask for permission if the app is not allowed





// We are calling this method to check the status of the permission

Private Boolean isReadStorageAllowed()) {{

// getting permission status

int results =, Clearly.Permission.READ_EXTERNAL_STORAGE);

// if permission is returned true

If ()results == package manager.permission granted)

The return True;

// false permission is allowed if not allowed

The return false;


// permission required

Private Zero requestStoragePermission()){{

If (),Clearly.Permission.READ_EXTERNAL_STORAGE)){{

// If the user has refused permission, your code will fall into this block

// Here you can explain why you need this permission

// explain here why you need this permission


// and finally ask for permission,New String[]{{Clearly.Permission.READ_EXTERNAL_STORAGE},STORAGE_PERMISSION_CODE);


// This method will be called when the user taps on permission or denial

@ Override

public Zero onRequestPcriResult()int request code, @Nonal String[] Permissions, @Nonal int[] Grant) {{

// checking the request code of our request

If()request code == STORAGE_PERMISSION_CODE){{

// if allowed

If()Grant.Length >0 && Grant[0] == package manager.permission granted){{

// Displaying a toast

Toast.Makeup()this,“Permission now you can read storage”,Toast.LENGTH_LONG).Display());


// Displaying the second toast if not allowed

Toast.Makeup()this,“Oops you just refused permission”,Toast.LENGTH_LONG).Display());





Source link

About dmtechnolab

Check Also

Simplified Coding

Android Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android …

Leave a Reply

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