Skip to content

An android library that simplifies the process of requesting android run time permissions.

License

Notifications You must be signed in to change notification settings

datanapps/AndroidRequestPermission

Repository files navigation

AndroidRequestPermission (java and kotlin) (Implement runtime permission utility)

An android library that simplifies the process of requesting android run time permissions.

alt text

Usage :

A. Add "javarequestpermission" directory for java project.

B. Add "kotlinrequestpermission" directory for kotlin project.

  1. Add string value in your string.xml file.
<string name="set_permission">Set Permisssion</string>
<string name="msg_permission">We can not continue without this permission.</string>
<string name="msg_permission_with_settings">We can not continue without this permission so goto settings screen and grant permission.</string>
  1. Extend RequestPermissionActivity for those activities which needs runtime permission.

public class MainActivity extends RequestPermissionActivity

  1. You have to add only below method at your action (befor method) =================================================================== private void checkPermissionAndOpenCamera(){

     setPermissionGrantedListener(new PermissionListener() {
         @Override
         public void onPermissionGranted(String permissionName) {
    
             // after got permission
             //openCamera();
         }
    
         @Override
         public void onPermissionDenied() {
             // do your code
         }
     });
    
     String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
     if (!hasPermissions(this, permissions)) {
         checkRunTimePermissions(permissions);
     } else {
    
         //openCamera();
    
     }
    

    }

  2. Full Code:

=============================================================================
public class MainActivity extends RequestPermissionActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    findViewById(R.id.open_camera).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkPermissionAndOpenCamera();
        }
    });
}

private void checkPermissionAndOpenCamera(){


    setPermissionGrantedListener(new PermissionListener() {
        @Override
        public void onPermissionGranted(String permissionName) {

            // after got permission
            openCamera();
        }

        @Override
        public void onPermissionDenied() {
            // do your code
        }
    });

    String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    if (!hasPermissions(this, permissions)) {
        checkRunTimePermissions(permissions);
    } else {

        openCamera();

    }
}



private void openCamera(){
    // camera
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, 101);
}

}

See

License

Copyright [2021] [datanapps]

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

About

An android library that simplifies the process of requesting android run time permissions.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published