Catalin Mares
Catalin Mares

Reputation: 105

onRequestPermissionResult not being called in AppCompatActivity

I am trying to request some permission for my app inside the launcher activity. This activity only checks for permissions and prompts the user asking for missing permission. What I want to do is after 1.5 seconds, if the user was prompted and accepted/denied the permissions required, the main activity is started, displaying the dashboard of the application. Now, the problem is that onRequestPermissionResult is not called no matter what I do and I don't understand where the problem comes from. I mention that the permissions are requested successfully, the user being prompted and being able to accept them or deny them, but the callback is not triggered somehow.

Here is my activity code:

public class LauncherActivity extends AppCompatActivity {
    public static final String TAG = LauncherActivity.class.getSimpleName();
    private boolean permissionsGiven;

    private static final String[] REQUIRED_PERMISSIONS =
            new String[] {
                    Manifest.permission.BLUETOOTH,
                    Manifest.permission.BLUETOOTH_ADMIN,
                    Manifest.permission.ACCESS_WIFI_STATE,
                    Manifest.permission.CHANGE_WIFI_STATE,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION
            };

    private static final int REQUEST_CODE_REQUIRED_PERMISSIONS = 1;

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

        if (!hasPermissions(this, REQUIRED_PERMISSIONS)) {
            permissionsGiven = false;
            Log.d(TAG, "onCreate: app does not have all the required permissions. Requesting permissions...");

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                ActivityCompat.requestPermissions(
                        this,
                        REQUIRED_PERMISSIONS,
                        REQUEST_CODE_REQUIRED_PERMISSIONS
                );
            }
        } else {
            permissionsGiven = true;
        }

        Runnable enterApplication = new Runnable() {
            @Override
            public void run() {
                while (!permissionsGiven);

                SharedPreferences loginPreferences = getApplicationContext().getSharedPreferences("LOGIN_DETAILS", MODE_PRIVATE);

                boolean signedIn = loginPreferences.getBoolean("signedIn", false);

                if (!signedIn) {
                    Log.d(TAG, "onCreate: user is not signed in. Sending him to login activity...");
                    sendUserToLoginActivity();
                } else {
                    sendUserToMainActivity();
                }
            }
        };

        Handler launcherHandler = new Handler();
        launcherHandler.postDelayed(enterApplication, 1500);
    }

    private static boolean hasPermissions(Context context, String... permissions) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }

        return true;
    }

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

        if (requestCode != REQUEST_CODE_REQUIRED_PERMISSIONS) {
            return;
        }

        permissionsGiven = true;

        for (int grantResult : grantResults) {
            if (grantResult == PackageManager.PERMISSION_DENIED) {
                Toast.makeText(this, "Missing permissions", Toast.LENGTH_LONG).show();
                finish();
                return;
            }
        }

        recreate();
    }

    private void sendUserToLoginActivity() {
        Log.d(TAG, "sendUserToLoginActivity: starting login activity...");
        Intent loginIntent = new Intent(LauncherActivity.this, SignInActivity.class);
        loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(loginIntent);
        finish();
    }

    private void sendUserToMainActivity() {
        Log.d(TAG, "sendUserToMainActivity: starting main activity...");
        Intent mainIntent = new Intent(LauncherActivity.this, MainActivity.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(mainIntent);
        finish();
    }
}

Can anyone help me understand what am I doing wrong and how can I trigger onRequestPermissionsResult so I can start the next activity inside the app?

Upvotes: 0

Views: 197

Answers (1)

blackapps
blackapps

Reputation: 9292

recreate();

I never saw that method.

Dont call it for a test and place a Toast instead.

I wonder how you know that it is not triggered.

Further i would not use a thread that is waiting for a variable to change its value.

Further you dont need threads at all. Just call the code in onCreate or onRequestPermissionsResult. You can place all that code in a function.

Upvotes: 1

Related Questions