`onRequestPermissionResult` 在 `AppCompatActivity` 中未被调用。

huangapple go评论69阅读模式
英文:

onRequestPermissionResult not being called in AppCompatActivity

问题

public class LauncherActivity extends AppCompatActivity {
    // ... (other code)

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

        // ... (other code)

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

                // ... (other code)
            }
        };

        // ... (other code)
    }

    // ... (other code)

    @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;

        // ... (other code)
    }

    // ... (other code)
}

Please note that the provided code is just a translation of the relevant parts. If you need assistance or further explanation about the code, feel free to ask.

英文:

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?

答案1

得分: 1

> recreate();

我从未见过那个方法。

不要为了测试而调用它,可以放一个Toast来代替。

我想知道你是如何知道它没有被触发的。

此外,我不会使用一个等待变量改变值的线程。

而且,你根本不需要线程。只需在onCreate或onRequestPermissionsResult中调用代码。你可以将所有代码放在一个函数中。
英文:

> 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.

huangapple
  • 本文由 发表于 2020年9月2日 00:33:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63691864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定