我试图在android中创建 float 覆盖空间。我有:
targetSdkVersion 30
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
我注册了一项服务
<service
android:name=".service.RecordService"
android:enabled="true"
android:foregroundServiceType="mediaProjection">
</service>
并运行它
Intent intent = new Intent(MainActivity.this, RecordService.class);
intent.putExtra("cmd","start service");
startService(intent);
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, intent);
所有这一切都在发生
mWindowManager = (WindowManager)getApplication().getSystemService(WINDOW_SERVICE);
mLayoutParams = new LayoutParams();
mLayoutParams.type = LayoutParams.TYPE_APPLICATION_OVERLAY;
mLayoutParams.format = PixelFormat.RGBA_8888;
mLayoutParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
mLayoutParams.gravity = Gravity.CENTER;
mLayoutParams.x = 0;
mLayoutParams.y = 0;
mLayoutParams.width = LayoutParams.WRAP_CONTENT;
mLayoutParams.height = LayoutParams.WRAP_CONTENT;
final LayoutInflater inflater = LayoutInflater.from(getApplication());
mFloatLayout = (FrameLayout)inflater.inflate(R.layout.layout_record,null);
mStartBtn = (TextView)mFloatLayout.findViewById(R.id.btn_start);
mStartBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(RecordService.this, RecordService.class);
intent.putExtra("cmd","start recording");
startService(intent);
}
});
mFloatLayout.setVisibility(View.INVISIBLE);
mWindowManager.addView(mFloatLayout, mLayoutParams);
在最后一行:( 我做错了什么?
我也尝试使用
mLayoutParams.type = LayoutParams.TYPE_PHONE;
mLayoutParams.type = LayoutParams.TYPE_SYSTEM_ALERT;
但有同样的问题:
Process: com.poqdev.screenrec, PID: 20517
java.lang.RuntimeException: Unable to create service com.poqdev.screenrec.service.RecordService: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@90e7698 -- permission denied for window type 2038
at android.app.ActivityThread.handleCreateService(ActivityThread.java:4204)
at android.app.ActivityThread.access$1500(ActivityThread.java:237)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@90e7698 -- permission denied for window type 2038
at android.view.ViewRootImpl.setView(ViewRootImpl.java:1092)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:409)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109)
at com.poqdev.screenrec.service.RecordService.createFloatView(RecordService.java:145)
at com.poqdev.screenrec.service.RecordService.onCreate(RecordService.java:66)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:4192)
我做错了什么?
最佳答案
您好,这可能有点晚了,但我遇到了同样的问题,我发现您必须在“设置”内授予您的应用程序“在其他应用程序上显示”权限。一旦您将其设置为“允许”,您就不会看到此错误。
关于Android:窗口类型 2038 的权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63654783/