Can anyone give some insights on how to request for user defined custom runtime permissions in Android Marshmallow ? I am working on Custom Content Provider which has a read permission and another app is trying to access the data using Custom Content Provider.
Asked
Active
Viewed 1,395 times
2

Puneet Chugh
- 93
- 10
-
I've already answered similar question. See [here](http://stackoverflow.com/a/40639430/3423932). – muthuraj Nov 16 '16 at 18:22
-
This is an older issue, but I ran into the 'Install Order Dependency' of custom permissions issue, and could not use other solutions such as 'signature' protection, as the other application was developed by another dev. You should be able to request custom permissions the same as you would system permissions, but make sure that the custom permissions have both a permission group, and a protection level of 'dangerous'. With both of those requirements, the system will provide a run time permission request. – AChez9 Sep 28 '17 at 18:14
2 Answers
2
I personally prefer using permissions library. Instead of large boilerplate code they tend to be effective and code is very precise
Refer this PermissionsDispatcher
@RuntimePermissions
public class MainActivity extends AppCompatActivity {
//sepcify permissions here
@NeedsPermission(Manifest.permission.CAMERA)
void showCamera() {
//work after permission is granted
}
@OnShowRationale({Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS})
void showRationaleForCamera(final PermissionRequest request) {
new AlertDialog.Builder(this)
.setMessage(R.string.permission_camera_rationale)
.setPositiveButton(R.string.button_allow, (dialog, button) -> request.proceed())
.setNegativeButton(R.string.button_deny, (dialog, button) -> request.cancel())
.show();
}
@OnPermissionDenied({Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS})
void showDeniedForCamera() {
//works when permissions denied
Toast.makeText(this, R.string.permission_camera_denied, Toast.LENGTH_SHORT).show();
}
@OnNeverAskAgain({Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS})
void showNeverAskForCamera() {
Toast.makeText(this, R.string.permission_camera_neverask, Toast.LENGTH_SHORT).show();
}
}

Victor
- 4,171
- 1
- 29
- 37
-
2Thanks for the example. Can you give me some example on how to use custom defined permissions. – Puneet Chugh Nov 15 '16 at 05:01
-
if u are asking for multiple permissions like read_storage and camera.. etc then i have update my answer u can define as many permissons u want – Victor Nov 15 '16 at 05:06
-
3These are all built-in permissions. I am looking for an example in which user has defined the permission. How to handle user defined custom permission that user defined ? – Puneet Chugh Nov 15 '16 at 05:18
-2
It works for me.
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
private boolean checkAndRequestPermissions() {
Context context=getApplicationContext();
int locationpermission = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
int readphonestatepermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE);
int permissionSendMessage = ContextCompat.checkSelfPermission(context,Manifest.permission.SEND_SMS);
int writepermission = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int readexternalstoragepermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);
List<String> listPermissionsNeeded = new ArrayList<>();
if (locationpermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (readphonestatepermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE);
}
if (permissionSendMessage != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.SEND_SMS);
}
if (writepermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (readexternalstoragepermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (!listPermissionsNeeded.isEmpty()) {
requestPermissions(listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_ID_MULTIPLE_PERMISSIONS) {
if (grantResults.length > 0) {
for (int i = 0; i < permissions.length; i++) {
if (permissions[i].equals(Manifest.permission.ACCESS_FINE_LOCATION)) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
Log.e("msg", "location granted");
}
} else if (permissions[i].equals(Manifest.permission.READ_PHONE_STATE)) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
Log.e("msg", "read phone state granted");
}
} else if (permissions[i].equals(Manifest.permission.SEND_SMS)) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
Log.e("msg", "sms granted");
}
}else if (permissions[i].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
Log.e("msg", "write external granted");
}
}else if (permissions[i].equals(Manifest.permission.READ_EXTERNAL_STORAGE)) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
Log.e("msg", "read external storage granted");
Toast.makeText(SplashActivity.this, "Please Restart the Application....... ", Toast.LENGTH_LONG).show();
}
}else if (permissions[i].equals(Manifest.permission.READ_LOGS)) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
Log.e("msg", "read logs granted");
}
}
}
}
}
}
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle v) {
// TODO Auto-generated method stub
super.onCreate(v);
setContentView(R.layout.activity_splash);
if (checkAndRequestPermissions()) {
Toast.makeText(MainActivity.this, "Granted all permissions", Toast.LENGTH_SHORT).show();
}
}
}

Amol Nage
- 85
- 10
-
3Thanks @Amol. This is an example of build-in permission. Can you provide me some example of dealing with user defined permissions. – Puneet Chugh Nov 15 '16 at 04:53