Solution 1 :
public static void PermissionCheck(Context context) {
int REQUEST_PERMISSIONS = 20;
if (!AppUtils.isPremissionsGranted(context)) {
AppUtils.requestAppPermissions(new
String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.WRITE_CONTACTS,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA,
Manifest.permission.READ_PHONE_STATE},
REQUEST_PERMISSIONS, context);
}
}
Solution 2 :
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUESTS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
// FIXME: Handle this case the user denied to grant the permissions
}
break;
}
default:
// TODO: Take care of this case later
break;
}
}
private void requestPermissions() {
List<String> requiredPermissions = new ArrayList<>();
if (ContextCompat.checkSelfPermission( this, Manifest.permission.WRITE_EXTERNAL_STORAGE )
!= PackageManager.PERMISSION_GRANTED) {
requiredPermissions.add( Manifest.permission.WRITE_EXTERNAL_STORAGE );
}
if (ContextCompat.checkSelfPermission( this, Manifest.permission.CAMERA )
!= PackageManager.PERMISSION_GRANTED) {
requiredPermissions.add( Manifest.permission.CAMERA );
}
if (!requiredPermissions.isEmpty()) {
ActivityCompat.requestPermissions( this,
requiredPermissions.toArray( new String[]{} ),
MY_PERMISSIONS_REQUESTS );
}
}
Problem :
private void CheckPermissions() {
RxPermissions.getInstance(SpalshActivity.this)
.request(
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.CAMERA,
android.Manifest.permission.CALL_PHONE
)
.subscribe(new Action1<Boolean>() {
@Override
public void call(Boolean aBoolean) {
initialize(aBoolean);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
}
});
}
private void initialize(boolean isAppInitialized) {
if (isAppInitialized) {
Thread background = new Thread() {
public void run() {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
};
background.start();
} else {
//If one Of above permission not grant show alert (force to grant permission)
AlertDialog.Builder builder = new AlertDialog.Builder(SpalshActivity.this);
builder.setTitle("Alert");
builder.setMessage("All permissions necessary");
builder.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
CheckPermissions();
}
});
builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.show();
}
}
Comments
Comment posted by Naveen kumar
We can check multiple permissions using String array.
Comment posted by Naveen kumar
Feel free to ask if you are having any issue.
Comment posted by Sunil Chaudhary
private static final int MY_PERMISSIONS_REQUESTS = 0; and from onCreateactivity() call requestPermissions();