问题
Today I installed latest version of Android Studio
I am learning Floating Widgets in Android
I started with applying this example
https://www.spaceotechnologies.com/android-floating-widget-tutorial/
it compiles ok
but when I run it in the emulator it crashes
giving me this error
08-28 22:52:02.932 7400-7400/com.asmgx.MyApp.MyApp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.asmgx.MyApp.MyApp, PID: 7400
java.lang.RuntimeException: Unable to create service com.asmgx.MyApp.MyApp.FloatWidgetService: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@7c93828 -- permission denied for window type 2002
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3544)
at android.app.ActivityThread.access$1300(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1666)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@7c93828 -- permission denied for window type 2002
at android.view.ViewRootImpl.setView(ViewRootImpl.java:822)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at com.asmgx.MyApp.MyApp.FloatWidgetService.onCreate(FloatWidgetService.java:36)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3532)
at android.app.ActivityThread.access$1300(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1666)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I tried resolving the issue and found this link
Unable to add window android.view.ViewRoot$W@44da9bc0 -- permission denied for this window type
they suggested adding this line to the manifest, which already added
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
anyone know why am i getting this?
PS. I used emulator with Android 28 and another with android 27
回答1:
This is occurring because the targetSdkVersion in the example and your targetSdkVersion are different. Use the flag TYPE_APPLICATION_OVERLAY instead of TYPE_PHONE in WindowManager.LayoutParams:
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
回答2:
This worked for me.. I'm sure it can be simplified, but it works perfectly otherwise.
Simply replace TYPE_PHONE with TYPE_APPLICATION_OVERLAY if (and only if) user is equal or above Oreo:
final WindowManager.LayoutParams params;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
} else {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
}
回答3:
By changing targetSdkVersion to 22, the issue will be solved partially. For devices above 22 you will get the same error. I found the following answer in stack overflow but didn't remember the link. You can try the following code:
private void createFloatView() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkDrawOverlayPermission();
} else {
createView();
}
}
public void checkDrawOverlayPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(getContext())) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getActivity().getPackageName()));
startActivityForResult(intent, CommonVariables.REQUEST_CODE);
} else {
createView();
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CommonVariables.REQUEST_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.canDrawOverlays(getContext())) {
createView();
}
}
}
}
private int CommonVariables.REQUEST_CODE = 5463 & 0xffffff00
回答4:
Simply replace
TYPE_PHONE,
in your code with
android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O ? TYPE_APPLICATION_OVERLAY : TYPE_PHONE,
来源:https://stackoverflow.com/questions/52059033/permission-denied-for-window-type-2002-in-android-studio