Managing Unknown App Source Permissions and Handling Back Navigation in Android

71 Views Asked by At

I'm working on an Android application that necessitates users to grant permission for installing the app from unknown sources. This requirement arises because the app occasionally requires updates and is not available on the Play Store.

The following code snippet showcases the requestPermissionToInstallUnknownApps method within an Android Activity. This method is responsible for requesting the necessary permission and has been tested on Android 12 (Samsung A10).

private void requestPermissionToInstallUnknownApps() {
    Timber.d("\nrequestPermissionToInstallUnknownApps");
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
                !getPackageManager().canRequestPackageInstalls()) {
            // Request permission to install unknown apps
            Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
            Uri uri = Uri.fromParts("package", getPackageName(), null);
            intent.setData(uri);
            startActivity(intent);
        }
    } catch (Exception ex) {
        Timber.e("\nrequestPermissionToInstallUnknownApps" +
                "\nException: " +
                "\n" + ex.getLocalizedMessage());
    }
}

The issue arises when the user, after granting permission in the App Settings screen, clicks the Back button, resulting in a crash of the application. My objectives are as follows:

  1. Ensure that the return value from the App Settings screen reaches the originating Activity.

  2. Enable the user, after granting permission in the App Settings screen and clicking the Back button, to return to the Activity where the initial request was made.

Any insights or solutions on how to achieve these goals would be greatly appreciated.

0

There are 0 best solutions below