我创建了一个aosp,在使用中它具有窗口管理器。我已经在清单中声明了窗口管理器权限,但是仍然出现无法添加窗口的错误。
public class BannerService extends Service {
private WindowManager mWinManager;
private WindowManager.LayoutParams mUpperBannerParams;
private WindowManager.LayoutParams mExpandSysMsgLayoutParam;
private WindowManager.LayoutParams mExpandLayoutParam;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
mWinManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
mUpperBannerParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
mUpperBannerParams.width = WindowManager.LayoutParams.MATCH_PARENT;
mUpperBannerParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
mUpperBannerParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
mUpperBannerParams.format = PixelFormat.TRANSLUCENT;
mUpperBannerParams.gravity = Gravity.TOP | Gravity.FILL_HORIZONTAL;
View bannerBackView = layout_inflater.inflate(R.layout.dummy_banner_background, null);
mExpandBannerWindowContainer = new FrameLayout(this) {
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
cancelResetExpandBanner(); // fail-safe:
return super.dispatchTouchEvent(ev);
}
};
mWinManager.addView(mExpandBannerWindowContainer, mUpperBannerParams); //getting error at 813th line is this because windowmanager in 9.
}
}
我什至添加了
但是我收到错误消息
12-13 05:28:55.218 1125 1125 E AndroidRuntime: java.lang.RuntimeException: Unable to create service jp.advop.banner.application.BannerService: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@36c0149 -- permission denied for window type 2010
12-13 05:28:55.218 1125 1125 E AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:3582)
12-13 05:28:55.218 1125 1125 E AndroidRuntime: at android.app.ActivityThread.access$1300(ActivityThread.java:200)
12-13 05:28:55.218 1125 1125 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
12-13 05:28:55.218 1125 1125 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
12-13 05:28:55.218 1125 1125 E AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
12-13 05:28:55.218 1125 1125 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6718)
12-13 05:28:55.218 1125 1125 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
12-13 05:28:55.218 1125 1125 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
12-13 05:28:55.218 1125 1125 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:917)
12-13 05:28:55.218 1125 1125 E AndroidRuntime: Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@36c0149 -- permission denied for window type 2010
12-13 05:28:55.218 1125 1125 E AndroidRuntime: at android.view.ViewRootImpl.setView(ViewRootImpl.java:822)
12-13 05:28:55.218 1125 1125 E AndroidRuntime: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
12-13 05:28:55.218 1125 1125 E AndroidRuntime: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
12-13 05:28:55.218 1125 1125 E AndroidRuntime: at jp.advop.banner.application.BannerService.onCreate(BannerService.java:813)
12-13 05:28:55.218 1125 1125 E AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:3570)
我什至也使用过前台服务。
public class ServiceBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
intent.setClass(context, BannerService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}
}
答案 0 :(得分:1)
从Android 8开始,具有SYSTEM_ALERT_WINDOW
权限的应用程序无法再使用TYPE_SYSTEM_ERROR
窗口类型在其他应用程序和系统窗口上方显示警报窗口。
相反,请使用名为TYPE_APPLICATION_OVERLAY
的新窗口类型。
参考:https://developer.android.com/about/versions/oreo/android-8.0-changes.html#cwt