3

I'm trying to implement runtime permission, but there are few problems when FragmentActivity receive onRequestPermissionResult(). The checkPermissions() method works very well, but onRequestPermissionResult() is never called after User allowed or denied permissions.

When system dialog box shown to request permission, MyActivity.onDestroy() is never called. Is it relevant to this problem?

I've already tried to follow this one and another one , but I think I missed something.

My device is Nexus 5X (Android 6.0) and Target SDK version is 23. Minimum support SDK version is 15. Of course, I wrote required permissions in AndroidManifest.xml and added v23 support libraries in build.gradle.

This is my code:

public class MyActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        checkPermissions();
    }

    private void checkPermissions(){
        String[] dangerousPermissions = {
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.GET_ACCOUNTS,
                Manifest.permission.WRITE_EXTERNAL_STORAGE, 
                Manifest.permission.RECORD_AUDIO
        };

        for(String permission : dangerousPermissions){
            if (!hasPermission(permission)) {
                Log.e("permission.debug", "need to grant " + permission);
                ActivityCompat.requestPermissions(this, dangerousPermissions, 4);
            }
        }
    }

    public boolean hasPermission(String permission){
        return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
    }

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

        Log.d("permission.debug", " request code " + requestCode);
        for (int i = 0; i < permissions.length; i++) {
            if (grantResults[i] == 0 && Manifest.permission.READ_PHONE_STATE.equals(permissions[i])) {
                initiate();
            }
        }
    }
}

This is log for my code:

E/permission.debug: need to grant android.permission.READ_PHONE_STATE
E/permission.debug: need to grant android.permission.GET_ACCOUNTS
E/permission.debug: need to grant android.permission.WRITE_EXTERNAL_STORAGE
E/permission.debug: need to grant android.permission.RECORD_AUDIO

Thank you.

Community
  • 1
  • 1
Eugene Kim
  • 41
  • 6

3 Answers3

1

Try to call requestPermissions directly rather than ActivityCompat.requestPermissions. Like below

if (checkNeededPermissions.size() > 0) {
    Log.d(TAG, "Checking permissions");
    requestPermissions(this, dangerousPermissions, 4);
}

EDIT

I don't see where you have defined the list checkNeededPermissions. Could you please put a log inside as above

Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45
1

I just found the solution.

When you declare an activity in AndroidManifest.xml with noHistory attribute, It will remove your Activity task when Activity disappeared from screen.

To receive onRequestPermissionResult() callback, you must guarantee the Activity resumed.

Community
  • 1
  • 1
Eugene Kim
  • 41
  • 6
1

Had the same issue. Fixed it by calling MyActivity.this instead of this at the call to ActivityCompat.requestPermissions

Ronen
  • 1,225
  • 13
  • 10