How to give and apply run time multiple permissions in android,
First yo have to add permissions in Manifest file like below.
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Then, you have to code for the check permissions,add button in activitymain.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
android:background="#e5ead3"
android:gravity="center" >
<Button
android:id="@+id/btn_do_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Permissions"
android:layout_gravity="center"/>
</LinearLayout>
After add button in xml file now add code in MainActivity.java file
import android.Manifest; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.os.Build; 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.LinearLayout; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Context mContext; private Activity mActivity; private Button btn_checkpermi; private static final int MY_PERMISSIONS_REQUEST_CODE = 123; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = getApplicationContext(); mActivity = MainActivity.this; btn_checkpermi = findViewById(R.id.btn_checkpermi); btn_checkpermi.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ checkPermission(); } } }); } protected void checkPermission(){ if(ContextCompat.checkSelfPermission(mActivity,Manifest.permission.CAMERA) + ContextCompat.checkSelfPermission( mActivity,Manifest.permission.READ_CONTACTS) + ContextCompat.checkSelfPermission( mActivity,Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ // when permissions not grantedif(ActivityCompat.shouldShowRequestPermissionRationale( mActivity,Manifest.permission.CAMERA) || ActivityCompat.shouldShowRequestPermissionRationale( mActivity,Manifest.permission.READ_CONTACTS) || ActivityCompat.shouldShowRequestPermissionRationale( mActivity,Manifest.permission.READ_EXTERNAL_STORAGE)){ // If we should give explanation of requested permissions // Show an alert dialog for request explanationAlertDialog.Builder builder = new AlertDialog.Builder(mActivity); builder.setMessage("Camera, Read Contacts and Read External" + " Storage permissions are required to do the task."); builder.setTitle("Please grant those permissions"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){ @Overridepublic void onClick(DialogInterface dialogInterface, int i) { ActivityCompat.requestPermissions( mActivity, new String[]{ Manifest.permission.CAMERA, Manifest.permission.READ_CONTACTS, Manifest.permission.READ_EXTERNAL_STORAGE }, MY_PERMISSIONS_REQUEST_CODE ); } }); builder.setNeutralButton("Cancel",null); AlertDialog dialog = builder.create(); dialog.show(); }else{ // request directly required permissionsActivityCompat.requestPermissions( mActivity, new String[]{ Manifest.permission.CAMERA, Manifest.permission.READ_CONTACTS, Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_CODE ); } }else { // Do your stuff here, permissions are already grantedToast.makeText(mContext,"Permissions already granted",Toast.LENGTH_SHORT).show(); } } @Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){ switch (requestCode){ case MY_PERMISSIONS_REQUEST_CODE:{ // When request is cancelled, the results array are emptyif( (grantResults.length >0) && (grantResults[0] + grantResults[1] + grantResults[2] == PackageManager.PERMISSION_GRANTED ) ){ // Permissions are grantedToast.makeText(mContext,"Permissions granted.",Toast.LENGTH_SHORT).show(); }else { // Permissions are deniedToast.makeText(mContext,"Permissions denied.",Toast.LENGTH_SHORT).show(); } return; } } } }
No comments:
Post a Comment
Comments