Add MarshMallow Runtime Permission in Android
Add Permission Manifest File
First Step is Add Permission in Android Manifest File.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-feature android:name="android.hardware.camera2" /> <uses-permission android:name="android.permission.CAMERA" />
I have Declare Manifest File Permission to Camera Permission And Add to Write External Storage And Internal Storage.
Now Second Step is Create Code to MainActivity File To Check Method On MainActivity OnCreate Method to Implements ad check boolean method to Permission Granted or Not.First Of All Design XML Page Create
MainActivity.xml File
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="com.example.dipl14.camerademo.MainActivity"> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Hello World"/> </LinearLayout>
Third Step to Create Java File And Implement Code to Runtime Permission to Access in User.
MainActivity.java
package com.bhaumik.runtimepermission; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.os.PersistableBundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.widget.Toast; import static android.Manifest.permission.CAMERA; import static android.Manifest.permission.READ_EXTERNAL_STORAGE; import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; public class RuntimePermission extends AppCompatActivity{ private static final int PERMISSION_REQUEST_CODE = 200; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!checkPermission()){ requestPermission(); } } private void requestPermission() { ActivityCompat.requestPermissions(RuntimePermission.this, new String[]{WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, CAMERA}, PERMISSION_REQUEST_CODE); } private boolean checkPermission() { int result = ContextCompat.checkSelfPermission(RuntimePermission.this, WRITE_EXTERNAL_STORAGE); int result1 = ContextCompat.checkSelfPermission(RuntimePermission.this, READ_EXTERNAL_STORAGE); int result2 = ContextCompat.checkSelfPermission(RuntimePermission.this, CAMERA); return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED && result2 == PackageManager.PERMISSION_GRANTED; } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode){ case PERMISSION_REQUEST_CODE : if (grantResults.length > 0){ boolean storages = grantResults[0] == PackageManager.PERMISSION_GRANTED; boolean Cameras = grantResults[0] == PackageManager.PERMISSION_GRANTED; if (storages && Cameras){ Toast.makeText(RuntimePermission.this, "Permission Granted", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(RuntimePermission.this, "Permission Denied", Toast.LENGTH_SHORT).show(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ if (shouldShowRequestPermissionRationale(WRITE_EXTERNAL_STORAGE)){ ShowMsgOkCancel("You need to allow access to the permissions", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, CAMERA}, PERMISSION_REQUEST_CODE); } } }); return; } } } } } } public void ShowMsgOkCancel(String msg, DialogInterface.OnClickListener listener){ new AlertDialog.Builder(RuntimePermission.this) .setMessage(msg) .setPositiveButton("OK", listener) .setNegativeButton("Cancel", null) .create() .show(); } }
0 comments:
Post a Comment