我已在Service中使用WindowManager,并且尝试从this发布和其他发布中查找解析,但是我仍然收到此错误消息。
无法添加窗口android.view.ViewRootImpl$W@b285d2e-此窗口类型的权限被拒绝
我已经在我的Manifest.xml上使用android.permission.SYSTEM_ALERT_WINDOW
<---当前版本,或者
android.permission.INTERNAL_SYSTEM_WINDOW
(此系统应用程序只能使用)
我的最低SDK为API 17。
错误按摩:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: id.nax.androidstatelite, PID: 16973
java.lang.RuntimeException: Unable to create service id.nax.androidstatelite.FloatingWindow: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@b285d2e -- permission denied for this window type
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3883)
at android.app.ActivityThread.access$2100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1909)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@b285d2e -- permission denied for this window type
at android.view.ViewRootImpl.setView(ViewRootImpl.java:936)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:337)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109)
at id.nax.androidstatelite.FloatingWindow.onCreate(FloatingWindow.java:56)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3873)
at android.app.ActivityThread.access$2100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1909)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我的服务:(我的意思是这段代码来自下载示例项目)
public class FloatingWindow extends Service {
WindowManager wm;
LinearLayout ll;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
ll = new LinearLayout(this);
ll.setBackgroundColor(Color.RED);
LinearLayout.LayoutParams layoutParameteres = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 400);
ll.setBackgroundColor(Color.argb(66,255,0,0));
ll.setLayoutParams(layoutParameteres);
final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
500, 200, WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
parameters.gravity = Gravity.CENTER | Gravity.CENTER;
parameters.x = 0;
parameters.y = 0;
Button stop = new Button(this);
stop.setText("Stop");
ViewGroup.LayoutParams btnParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
stop.setLayoutParams(btnParameters);
ll.addView(stop);
wm.addView(ll, parameters);
ll.setOnTouchListener(new View.OnTouchListener() {
WindowManager.LayoutParams updatedParameters = parameters;
double x;
double y;
double pressedX;
double pressedY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = updatedParameters.x;
y = updatedParameters.y;
pressedX = event.getRawX();
pressedY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
updatedParameters.x = (int) (x + (event.getRawX() - pressedX));
updatedParameters.y = (int) (y + (event.getRawY() - pressedY));
wm.updateViewLayout(ll, updatedParameters);
default:
break;
}
return false;
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wm.removeView(ll);
stopSelf();
System.exit(0);
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
stopSelf();
}
}
我更喜欢/从this和this复制代码
我将该代码复制了一段时间,在项目下载中已经有app-debug.apk(清单中使用android.permission.SYSTEM_ALERT_WINDOW
),并且尝试正常运行,但是当我在项目中使用该代码时,我那错误的按摩。
我还必须使用什么权限?
答案 0 :(得分:0)
这个问题已经由我自己解决