-1

I have implemented Android Runtime Permissions in my Android App. I am also able to check if a permission is allowed or denied at runtime. But I want to know if there is anyway that if a user first denied Permission and then allowed it via app permissions menu in Settings. (Assume a service is always running to check if any changes occur)

Mehul Kanzariya
  • 888
  • 3
  • 27
  • 58
  • You can always check for the permission when you want it, while the user is using your app. Why do you want to detect change in permissions in the background? – Sony Mar 07 '17 at 10:42

2 Answers2

2

One little trick for I am also able to check if a permission is allowed or denied at runtime. Save this detail(denied or allowed) and then if user changed something from settings you can easily compare previous details in permission function of your app.

For ex: your saved details was denied and then you allowed permission through settings after that if you open your app in permission function you can see your saved detail was denied but now you have permission, for allowed vice-versa.

Pushpendra
  • 2,791
  • 4
  • 26
  • 49
0

I use the following helper class for checking and requesting permissions

public final class Permissions {

public static final int INTERNAL_STORAGE_REQUEST_CODE = 1;
public static final int FINE_LOCATION_REQUEST_CODE = 2;
private static final int COARSE_LOCATION_REQUEST_CODE = 3;

public static final String READ_EXTERNAL_STORAGE = Manifest.permission.READ_EXTERNAL_STORAGE;
public static final String ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
public static final String ACCESS_COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;


public static boolean havePermissionFor(Context context, String permission) {
    return Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
            ActivityCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;

}


public static void showPermissionDetails(String message, Context context, DialogInterface.OnClickListener okListener) {
    M.showAlert(context, "Request for permission", message, "ALLOW", "DON'T ALLOW", okListener, null, false);
}

public static void requestForPermissions(Activity activity, String[] permissions, int permissionRequestCode) {
    ActivityCompat.requestPermissions(activity, permissions, permissionRequestCode);
}

public static void requestPermissionForFragment(Fragment fragment, String[] permissions, int permissionRequestCode) {
    fragment.requestPermissions(permissions, permissionRequestCode);
}

public static boolean shouldShowPermissionRationale(Activity activity, String permission) {
    return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
}


public ArrayList<String> getRequiredPermissions(Context context, String... permissions) {
    ArrayList<String> requiredPermissionsList = new ArrayList<>();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return requiredPermissionsList;
    for (String permission : permissions) {
        if (!havePermissionFor(context, permission))
            requiredPermissionsList.add(permission);
    }
    return requiredPermissionsList;

}
}
Sony
  • 7,136
  • 5
  • 45
  • 68