Giter Club home page Giter Club logo

perm's Introduction

#Perm for Android#

Perm is a library that makes it simple to check and request Android Permissions at runtime (like Camera or GPS) for Apps targetting Android 6 or more.

Usage

To Check and Request a permission, simply create a Perm instance, and use isGranted, isDenied or askPermission methods:

public class MainActivity extends AppCompatActivity {

    private static final int PERMISSION_CAMERA_REQUEST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_activity);

        Perm cameraPermission = new Perm(this, Manifest.permission.CAMERA);
        if (cameraPermission.isGranted()) {
            Toast.makeText(this, "Camera Permission already granted", Toast.LENGTH_LONG).show();
        } else if (cameraPermission.isDenied()) {
            Toast.makeText(this, "Camera Permission already denied", Toast.LENGTH_LONG).show();
        } else {
            cameraPermission.askPermission(PERMISSION_CAMERA_REQUEST);
        }
    }
}

To handle the result of the request, create a PermResult in the onRequestPermissionsResult of your Activity, and use isGranted, or isDenied methods.

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == PERMISSION_CAMERA_REQUEST) {
        PermResult cameraPermissionResult = new PermResult(permissions, grantResults);
        if (cameraPermissionResult.isGranted()) {
            Toast.makeText(this, "Camera Permission granted", Toast.LENGTH_LONG).show();
        } else if (cameraPermissionResult.isDenied()) {
            Toast.makeText(this, "Camera Permission denied", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "Camera Permission cancelled", Toast.LENGTH_LONG).show();
        }
    }
}

Don't forget to add the required permissions in your AndroidManifest.xml file! For example

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.myapp">

    <uses-permission
        android:name="android.permission.CAMERA"
        android:required="false" />
    ...

Installation with gradle

Add the following maven{} line to your PROJECT build.gradle file

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }    // add this line
    }
}

Add the libary dependency to your APP build.gradle file

dependencies {
    compile 'com.github.smart-fun:Perm:1.0.0'    // add this line
}

##License##

Copyright 2016 Arnaud Guyon

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

perm's People

Contributors

smart-fun avatar

Watchers

James Cloos avatar thu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.