Search code examples
androidandroid-6.0-marshmallowruntime-permissions

Runtime Permission at Startup


I have a flashlight that needs CAMERA and WRITE_SETTINGS permissions, I need to first handle these permissions which I already know how to do it, Then if it is granted, for example, start my main activity... (sorry, I am new in android)


Solution

  • Add this library from github in your gradle file

    Than add this code

    PermissionListener permissionlistener = new PermissionListener() {
        @Override
        public void onPermissionGranted() {
            //The user have conceded permission
            Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onPermissionDenied(ArrayList<String> deniedPermissions) {
            //close the app or do whatever you want
            Toast.makeText(MainActivity.this, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
        }
    };
    
    new TedPermission(this)
    .setPermissionListener(permissionlistener)
    .setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
    .setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
    .check();
    

    Or if you prefer do it natively, check in the google docs