更新:问题已解决。
警告:此问题完全没有上面提到的答案。
问题是,应该有docker run -v
而不是WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
。
如果您遇到相同的问题,我建议在Java类中寻求许可,如注释中0X0nosugar提到的链接所示。
我正在运行一项服务,该服务会创建一个包含一些信息的透明浮动窗口,用户可以将其拖放到任何地方。
到目前为止,添加所有视图后,我的服务崩溃了,并且出现了此错误。
TYPE_PHONE
我的java.lang.RuntimeException:
Unable to create service afm.dragger.Dragger:
android.view.WindowManager$BadTokenException:
Unable to add window android.view.ViewRootImpl$W@ba63d06 -- permission denied for window type 2002
是21,而minSdkVersion
是27,我的手机上已经有sdk 27。
这是我的targetSdkVersion
:
AndroidManifest
这是我的<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".Dragger"/>
</application>
:
Dragger.class
最后,这是我的@Override
public void onCreate() {
super.onCreate();
WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setBackgroundColor(Color.parseColor("#33FFFFFF"));
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(linearParams);
Button button = new Button(this);
button.setBackground(getResources().getDrawable(R.drawable.button_shape));
button.setTextColor(getResources().getColor(android.R.color.white));
button.setTextSize(14);
button.setText("Stop Service");
ViewGroup.LayoutParams buttonParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(buttonParams);
linearLayout.addView(button, buttonParams);
final WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams(350, 250, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
windowParams.x = 0;
windowParams.y = 0;
windowParams.gravity = Gravity.START|Gravity.TOP;
manager.addView(linearLayout, windowParams);
linearLayout.setOnTouchListener(new View.OnTouchListener() {
private WindowManager.LayoutParams updatedParams = windowParams;
int x, y;
float touchedX, touchedY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = updatedParams.x;
y = updatedParams.y;
touchedX = event.getRawX();
touchedY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
updatedParams.x = (int) (x + (event.getRawX() - touchedX));
updatedParams.y = (int) (y + (event.getRawY() - touchedY));
manager.updateViewLayout(linearLayout, updatedParams);
default:
break;
}
return false;
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
manager.removeView(linearLayout);
stopSelf();
}
});
}
:
MainActivity
设置为:
start.setOnClickListener