Hello friends, welcome to this new tutorial. Today I will show you Android Marshmallow Permissions Example. One of the major changes in Android Marshmallow is the new permission system. In earlier versions we were declaring the permission in the AndroidManifest.xml file. But with Android Marshmallow we need to ask the permission at run time.  In this post I will show you a simple Android Marshmallow Permissions Example. So lets begin.
Android Marshmallow Permissions Example Video
You can watch this video before moving to the Android Marshmallow Permissions example. This is video is the demonstration of the final output.
Android Marshmallow Permissions Example Project
- Open Android Studio and create a new Android Project. Select Empty Activity come to your project window. After these steps (we do it always :P).
- In this example I will try to ask the permission for READING EXTERNAL STORAGE. So go to your AndroidManifest.xml file and add this permission.
1 2 3 |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> |
- Although we have added the permission in our Manifest but if the device is running Android Marshmallow we need to ask the permission at run time. So for asking permission at run time I will create a button. So you create a simple button at your activity_main.xml. Or copy the following code and paste it to your activity_main.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="net.simplifiedcoding.runtimepermission.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Request Storage Permission" android:id="@+id/buttonRequestPermission" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout> |
- After this come to your MainActivity.java and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
package net.simplifiedcoding.runtimepermission; import android.Manifest; 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.support.v7.view.menu.ActionMenuItemView; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { //Our button private Button buttonRequestPermission; //Permision code that will be checked in the method onRequestPermissionsResult private int STORAGE_PERMISSION_CODE = 23; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initializing button buttonRequestPermission = (Button) findViewById(R.id.buttonRequestPermission); //Adding a click listener buttonRequestPermission.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //First checking if the app is already having the permission if(isReadStorageAllowed()){ //If permission is already having then showing the toast Toast.makeText(MainActivity.this,"You already have the permission",Toast.LENGTH_LONG).show(); //Existing the method with return return; } //If the app has not the permission then asking for the permission requestStoragePermission(); } }); } //We are calling this method to check the permission status private boolean isReadStorageAllowed() { //Getting the permission status int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE); //If permission is granted returning true if (result == PackageManager.PERMISSION_GRANTED) return true; //If permission is not granted returning false return false; } //Requesting permission private void requestStoragePermission(){ if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_EXTERNAL_STORAGE)){ //If the user has denied the permission previously your code will come to this block //Here you can explain why you need this permission //Explain here why you need this permission } //And finally ask for the permission ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},STORAGE_PERMISSION_CODE); } //This method will be called when the user will tap on allow or deny @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { //Checking the request code of our request if(requestCode == STORAGE_PERMISSION_CODE){ //If permission is granted if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ //Displaying a toast Toast.makeText(this,"Permission granted now you can read the storage",Toast.LENGTH_LONG).show(); }else{ //Displaying another toast if permission is not granted Toast.makeText(this,"Oops you just denied the permission",Toast.LENGTH_LONG).show(); } } } } |
- Thats it now you can run your application. But make sure the emulator you are using is running Android Marshmallow.
- You can also download the source code from GitHub. Go to the link given below to get my source.
[sociallocker id=1372] Android Marshmallow Permissions Example Source Download [/sociallocker]
So thats all for this Android Marshmallow Permissions Example friends. I have explained all the codes with comments. But still if you are having any kind of confusion regarding this Android Marshmallow Permissions Example feel free to leave your comments. I will try to reply you all ASAP. Thank You 🙂