I am implementing an APK installer for my Android app. The workflow on Android 7 executes flawlessly. However, Android 8+ is running into permission issues. Expecting this, the app is set up to detect this and asks the user for permission (REQUEST_INSTALL_PACKAGES).
Here's where I ran into my issue; even though I am granting the permission, my app does not seem to think so. This is how I handle when the user comes back from the permission settings:
ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<>() {
@Override
public void onActivityResult(ActivityResult result) {
runOnUiThread(() -> {
if (appHasInstallPackagePermission()) {
showInstallationStep();
} else {
manuallyRecheckedInstallPermission = true;
showPermissionRequiredPage();
}
});
}
}
);
It does not matter if I turn on the permission, it is always the else branch that executes, so I am back to the permissions page of my app... More of the relevant code:
if (appHasInstallPackagePermission()) {
showInstallationStep();
} else {
showPermissionRequiredPage();
}
private void showPermissionRequiredPage() {
String appName = launcherPackageInfo.getAppName();
titleTextView.setText(getString(R.string.permission_required_title));
if (manuallyRecheckedInstallPermission) {
descriptionTextView.setText("...");
} else {
descriptionTextView.setText("...");
}
permissionButton.setOnClickListener(new View.OnClickListener() {
@Override
@RequiresApi(api = Build.VERSION_CODES.O)
public void onClick(View v) {
requestInstallPackagesPermission();
permissionButton.setText("...");
}
});
permissionButton.setText(getString(R.string.grant_permission_button_text));
permissionButton.setVisibility(View.VISIBLE);
permissionButton.requestFocus();
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void requestInstallPackagesPermission() {
Intent settingsIntent = new Intent(
Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,
Uri.parse("package:" + getPackageName())
);
activityResultLauncher.launch(settingsIntent);
}