2017年6月7日 星期三

Crash when set TYPE_DISPLAY_OVERLAY type by inflection

Error:
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@71dc214 -- permission denied for window type 2026

Solution:
TYPE_DISPLAY_OVERLAY需要
android.Manifest.permission.INTERNAL_SYSTEM_WINDOW

AndroidManifest.xml
<permission android:name="android.permission.INTERNAL_SYSTEM_WINDOW" android:protectionLevel="signature" />

app要 sign platform key才可使用


Call stack:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hueijheyu.reflectiondemo/com.example.hueijheyu.reflectiondemo.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@71dc214 -- permission denied for window type 2026
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:154)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                    Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@71dc214 -- permission denied for window type 2026
                                                       at android.view.ViewRootImpl.setView(ViewRootImpl.java:703)
                                                       at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
                                                       at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
                                                       at android.app.Dialog.show(Dialog.java:322)
                                                       at com.example.hueijheyu.reflectiondemo.MainActivity.onCreate(MainActivity.java:39)
                                                       at android.app.Activity.performCreate(Activity.java:6723)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:154)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)