I'm fixing an android app not developed by me, a few days ago the client asked me to set the targetSdkVersion to 26 (before it was 14), after I did I noticed that the app crashes in some cases, for example crashes when a ProgressDialog is shown. This is the part of the code with this problem:
mProgressDialog.setTitle(getString(R.string.downloading_data));
mProgressDialog.setMessage(mAlertMsg);
Drawable icon = getActivity().getDrawable(android.R.drawable.ic_dialog_info);
icon.setColorFilter(Color.parseColor("#00cbac"), PorterDuff.Mode.SRC_ATOP);
mProgressDialog.setIcon(icon);
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false);
mProgressDialog.setButton(getString(R.string.cancel), loadingButtonListener);
mProgressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
mProgressDialog.show();
When the last line is executed, the app crashes and appears this error in Logcat:
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@b784d3a -- permission denied for window type 2003
This problem occurs when the app is running in devices that have Android 8 and 9.
I looked for solutions to similar problems and I found that it would be better to use WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
instead of TYPE_SYSTEM_ALERT
, then I modified the second-last line of the code I wrote in this post but it does not change anything, the app crashes the same and in the log This error appears:
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@b784d3a -- permission denied for window type 2038
In the Manifest I have the permission:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
I have also enabled all permissions of the app from the device settings. How can I solve this problem?