Android runtime permission with lower version compatibility

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app’s functionality; for example, a user could choose to give a camera app access to the camera but not to the device location. The user can revoke the permissions at any time, by going to the app’s Settings screen.

Though this feature gives user many advantage to control app’s activity but maintaining runtime permission is quite tough. Google has provided a simple library to handle runtime permission. you can check this from this google sample. We will see how to use this library in the real project. The full project is available in Github. Project link here Simple Permission.

Step 1:

EasyPermissions is installed by adding the following dependency to your build.gradle file:

dependencies {
compile 'pub.devrel:easypermissions:0.2.1'
}

Step 2:

To begin using EasyPermissions, have your Activity (or Fragment) override the onRequestPermissionsResultmethod:

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

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

// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
}

Optionally, for a finer control, you can have your Activity / Fragment implement the PermissionCallbacksinterface.

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {

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

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

// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}

@Override
public void onPermissionsGranted(int requestCode, List<String> list) {
// Some permissions have been granted
// ...
}

@Override
public void onPermissionsDenied(int requestCode, List<String> list) {
// Some permissions have been denied
// ...
}
}

Done!! now it’s time to call for a permission. Here a process when user will click a button

//button click
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_shoot) {
EasyPermissions.requestPermissions(this, "Need Camera Permission", CAMERA_PERMISSION,
Manifest.permission.CAMERA);
}
}

for full project check Github.