Skip to content
simple Android permission request
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
GrantAndroidPermission
app
gradle/wrapper
screenshot
.gitignore
README.md
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle

README.md

Android_M_requestPermissions

  1. simple Android permission request
  2. support SYSTEM_ALERT_WINDOW and WRITE_SETTINGS permission request
  3. PermissionChecker.checkSelfPermission() check permission

GrantAndroidPermission library

request_permission

Gradle

Get library from oss.sonatype.org.io

repositories {
    
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }

}

dependencies {
    compile compile 'com.github.captain-miao:grantap:1.0.3'
}

check permission use CheckPermission

    //SYSTEM_ALERT_WINDOW write permission
    String[] systemAlertWindowPermission = new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW};
    public void showRequestPermissionAlertWindow(View view) {
        CheckPermission
                .from(this)
                .setPackageName(getPackageName())
                .setPermissions(systemAlertWindowPermission)
                .setRationaleConfirmText("Request SYSTEM_ALERT_WINDOW")
                .setDeniedMsg("The SYSTEM_ALERT_WINDOW Denied")
                .setPermissionListener(new PermissionListener() {
                    @Override
                    public void permissionGranted() {
                        Toast.makeText(MainActivity.this, "SYSTEM_ALERT_WINDOW Permission Granted", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void permissionDenied() {
                        Toast.makeText(MainActivity.this, "SYSTEM_ALERT_WINDOW Permission Denied", Toast.LENGTH_SHORT).show();
                    }
                })
                .check();
    }

check permission use CheckAnnotatePermission

    @PermissionCheck()
    String[] normalPermission = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION};
    public void showPermissionInternet(View view) {
        CheckAnnotatePermission
                .from(this, this)
                .check();
    }
    @PermissionGranted()
    public void permissionGranted() {
        Toast.makeText(this, "ACCESS_COARSE_LOCATION Permission Granted", Toast.LENGTH_SHORT).show();
    }
    @PermissionDenied()
    public void permissionDenied() {
        Toast.makeText(this, "ACCESS_COARSE_LOCATION Permission Denied", Toast.LENGTH_SHORT).show();
    }

about targetSdkVersion

PermissionChecker ContextCompat

android {
    //targetSdkVersion 23,Context.checkSelfPermission() check permission
    targetSdkVersion 23
}
//or
android {
    //targetSdkVersion 22,PermissionChecker.checkSelfPermission() check permission
    targetSdkVersion 22
}

### about permission check | ANDROID | targetSdkVersion | Context.checkSelfPermission() | PermissionChecker.checkSelfPermission() | requestPermission | | ------------- | ------------- | ------------- | ----------------------------- | ------------- | | Android5.0 | <= 22 | GRANTED | GRANTED | no | | Android5.0 | >= 23 | GRANTED | GRANTED | no | | Android6.0 | <= 22 | GRANTED | GRANTED
DENIED_APP_OP | yes | | Android6.0 | >= 23 | GRANTED
DENIED | GRANTED
DENIED | yes |

##Thanks

  • TedPermission - Easy check permission library for Android Marshmallow
  • PermissionGen - Android API easy to use permission for Android M



requestPermissions Android example

0. three permission types

  1. Normal permissions
    the system automatically grants the permission to the app
  2. Dangerous permissions
    has to explicitly grant the permission to the app.
  3. Other permissions:
    Random Musings on the Android 6.0 SDK

1. Normal permission(such as INTERNET)

If your app lists normal permissions in its manifest (that is, permissions that don't pose much risk to the user's privacy or the device's operation), the system automatically grants those permissions.

2. Dangerous permissions(such as ACCESS_COARSE_LOCATION)

dangerous permissions has to explicitly grant the permission to the app.

    //dangerous permission
    public void showRequestPermissionAccessCoarseLocation(View view) {
        Log.i(TAG, "Checking permission.");
        boolean hasSelfPermission = PermissionUtils.hasSelfPermissions(this, dangerousPermission);
        if(hasSelfPermission) {
            new AlertDialog.Builder(this)
                    .setTitle("showPermissionLocation")
                    .setMessage(settingsPermission[0] + "\r\n" + "granted")
                    .setPositiveButton(android.R.string.ok, null)
                    .show();
        } else {
            boolean showRationale = PermissionUtils.shouldShowRequestPermissionRationale(this, dangerousPermission);
            // Provide an additional rationale to the user if the permission was not granted
            // and the user would benefit from additional context for the use of the permission.
            // For example if the user has previously denied the permission.
            if(showRationale){
                new AlertDialog.Builder(this)
                        .setTitle("needs location access")
                        .setMessage("Please grant location access :-)")
                        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                requestPermissions(dangerousPermission, ACCESS_COARSE_LOCATION_REQUEST_CODE);
                            }
                        })
                        .show();
            } else {
                //Build.VERSION.SDK_INT >= 23 Activity and Fragment use requestPermissions()
                requestPermissions(dangerousPermission, ACCESS_COARSE_LOCATION_REQUEST_CODE);
                // or
                //ActivityCompat.requestPermissions(this,
                //                            dangerousPermission,
                //                            ACCESS_COARSE_LOCATION_REQUEST_CODE);
            }
        }

    }


    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String permissions[], @NonNull int[] grantResults) {
        switch (requestCode) {
            case ACCESS_COARSE_LOCATION_REQUEST_CODE: {

                boolean hasSelfPermission = PermissionUtils.verifyPermissions(grantResults);
                new AlertDialog.Builder(this)
                        .setTitle("showPermissionLocation")
                        .setMessage(dangerousPermission[0] + "\r\n" + (hasSelfPermission ? "granted" : "not granted"))
                        .setPositiveButton(android.R.string.ok, null)
                        .show();

            }
            break;
        }

    }

3. Other permissions(such as WRITE_SETTINGS)

    //system settings permission
    public void showRequestPermissionWriteSettings(View view) {
        // for Settings.ACTION_MANAGE_WRITE_SETTINGS: Settings.System.canWrite
        // CommonsWare's blog post:https://commonsware.com/blog/2015/08/17/random-musings-android-6p0-sdk.html
        boolean hasSelfPermission = Settings.System.canWrite(this);
        if(hasSelfPermission) {
            new AlertDialog.Builder(this)
                    .setTitle("showPermissionWriteSettings")
                    .setMessage(settingsPermission[0] + "\r\n" + "granted")
                    .setPositiveButton(android.R.string.ok, null)
                    .show();
        } else {
            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
                            Uri.parse("package:" + getPackageName()));
                    startActivityForResult(intent, WRITE_SETTINGS_REQUEST_CODE);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == WRITE_SETTINGS_REQUEST_CODE) {
            boolean hasSelfPermission = Settings.System.canWrite(this);
            new AlertDialog.Builder(this)
                    .setTitle("showPermissionInternet")
                    .setMessage(settingsPermission[0] + "\r\n" + (hasSelfPermission ? "granted" : "not granted"))
                    .setPositiveButton(android.R.string.ok, null)
                    .show();

        }
    }



##License

Copyright 2016 captain_miao

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.```
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.