Tuesday, 6 March 2018

Easy permission in Android

In this Android tutorial, we are going to learn how to use android Easy Permission Library in our project to check and request for android version 6 and above run-time permissions.EasyPermissions is a wrapper library to simplify basic system permissions logic when targeting Android M or higher. Using EasyPermissions to check if the app already has the required permissions. This method can take any number of permissions as its final argument.

In ManiFest File: -

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

First Add Dependency in build.gradle: -

dependencies {

compile 'pub.devrel:easypermissions:0.2.0'

}

Second Make a Java File: -

package com.example.safal.sahilapp.easyPermission;
import android.Manifest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.example.safal.sahilapp.R;
import java.util.List;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;
public class EasyPermission extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
private static final String TAG = "MainActivity";
private static final int RC_CAMERA_PERM = 123;
private static final int RC_LOCATION_CONTACTS_PERM = 124;
private static final int RC_SETTINGS_SCREEN = 125;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_easy_permission);
findViewById(R.id.button_location_and_wifi).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationAndContactsTask();
}
});
}
public void locationAndContactsTask() {
String[] perms = { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_CONTACTS, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE  };

// If you need more permission on this braces
if (EasyPermissions.hasPermissions(this, perms)) {
        } else {
            EasyPermissions.requestPermissions(this, "This app needs access to your location and contacts to know where and who you are.",
RC_LOCATION_CONTACTS_PERM, perms);
}
}
@Override
public void onPermissionsGranted(int requestCode, List<String> perms) {
Log.d(TAG, "onPermissionsGranted:" + requestCode + ":" + perms.size());
}
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
new AppSettingsDialog.Builder(this, "This app may not work correctly without the requested permissions. Open the app settings screen to modify app permissions.")
.setTitle("Permissions Required")
.setPositiveButton("Settings")
.setNegativeButton("Cancel", null)
.setRequestCode(RC_SETTINGS_SCREEN)
.build()
.show();
}
}
}

 

No comments:

Post a Comment