Breaking News

How to request multiple Runtime permission in Android App




When Google updates it's android OS version to Marshmallow 6.0, then it requires special permission access on runtime. For example, if you want to download something from an app, you must accept storage permission which is requested by the developer. Although, you can also enable all permissions from the application information section through the setting of your mobile.

But after marshmallow 6.0 you have to request permission to the user.


In this blog, we will tell you how to request needed permission to user, you can request multiple permission with that. and request of permission is on the button click method so that anywhere you want to request just call this method


For requesting permission, we create a new Permission activity.


Permissionactivity.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   tools:context=".Permission_Activity">  
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Permission"  
     android:id="@+id/getpermission"/>  
 </androidx.constraintlayout.widget.ConstraintLayout>  

here, we create a button where we perform a click action of requesting permission.


Then we create a permission method and call permission on button click.


PermissionActivity.java

 package studio.harpreet.mybrowser;  
 import androidx.appcompat.app.AppCompatActivity;  
 import androidx.core.app.ActivityCompat;  
 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;  
 import android.Manifest;  
 import android.app.Activity;  
 import android.content.Context;  
 import android.content.pm.PackageManager;  
 import android.os.Build;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.Button;  
 public class Permission_Activity extends AppCompatActivity {  
   Button btn;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_permission_);  
     btn = findViewById(R.id.getpermission);  
     btn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         int permission_All = 1;  
         String[] permission = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};  
         if(!hasPermission(Permission_Activity.this,permission))  
         {  
           ActivityCompat.requestPermissions(Permission_Activity.this,permission,permission_All);  
         }  
       }  
     });  
   }  
   public static boolean hasPermission(Context context, String... permissions)  
   {  
     if(Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M &amp;&amp;  
     context!=null &amp;&amp; permissions!=null)  
     {  
       for(String permission : permissions)  
       {  
         if(ActivityCompat.checkSelfPermission(context,permission) != PackageManager.PERMISSION_GRANTED)  
         {  
           return false;  
         }  
       }  
     }  
     return true;  
   }  
 }  

Here, we create a method called hasPermission in which we check if permission is got or not. If storage permission is not granted, then we request permission from the user.


Don't forget to add that permission to your AndroidManifest.


AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="studio.harpreet.mybrowser">  
   <uses-permission android:name="android.permission.INTERNET" />  
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />  
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  




Follow us for more posts like this, 
Subscribe Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments