Wednesday, March 11, 2020

Run time Permission in Android Studio.

1). Androidmanifest.xml 

The permissions that the app needs in order to access protected parts of the system or other apps. It also declares any permissions that other apps must have if they want to access content from this app. 


<manifest xmlns:android="http://schemas.android.com/apk/res/android"   
 package="com.example.mypermissiontest">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>   
 <application          
        android:allowBackup="true"        
        android:icon="@mipmap/ic_launcher"        
        android:label="@string/app_name"        
        android:roundIcon="@mipmap/ic_launcher_round" 
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>                
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />     
       </intent-filter> 
       </activity>
    </application>
</manifest>



2).activity_main.xml

layout file is compiled into a view resource


<?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"    
    tools:context=".MainActivity">
    <Button        
       android:id="@+id/btncheckpermission" 
       android:layout_width="wrap_content"        
       android:layout_height="wrap_content"        
       android:layout_centerInParent="true"        
       android:text="Check Permission" />
</RelativeLayout> 

3). MainActivity.java

  • Check whether permission is already granted or not. If permission isn’t already granted, request user for the permission: In order to use any service or feature, the permissions are required. Hence we have to ensure that the permissions are given for that. If not, then the permissions are requested.
  • Check for permissions: Beginning with Android 6.0 (API level 23), the user has the right to revoke permissions from any app at any time, even if the app targets a lower API level. So to use the service, the app needs to check for permissions every time.

  • method in the above syntax, we need to prompt the user for that permission. Android provides several methods that can be used to request permission, such as requestPermissions().



package com.example.mypermissiontest;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button btncheckpermission;   
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btncheckpermission=findViewById(R.id.btncheckpermission);
        btncheckpermission.setOnClickListener(this);    }

    @Override    public void onClick(View v) {
        if (v.getId()==R.id.btncheckpermission){
            isStoragepermission();        }
    }

    public boolean isStoragepermission(){
        if (Build.VERSION.SDK_INT>=23)
        {

            if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)== 
                PackageManager.PERMISSION_GRANTED                
                && ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE)== 
                PackageManager.PERMISSION_GRANTED) 
            {
                return true;
            }
            else {
                ActivityCompat.requestPermissions(this,new String[]
                {Manifest.permission.WRITE_EXTERNAL_STORAGE,
                 Manifest.permission.READ_EXTERNAL_STORAGE},1); 
               return false;          
                 }
        }else {
            return true;     
        }
    }

    @Override    
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
    @NonNull int[] grantResults)
    {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
        if (grantResults.length>0){
            if (grantResults[0]==PackageManager.PERMISSION_GRANTED){
                Toast.makeText(this,"Permission Granted",Toast.LENGTH_SHORT).show();            }else {
                Toast.makeText(this,"Permission Not Granted",Toast.LENGTH_SHORT).show();
            }
        }
    }



 

No comments:

Post a Comment