无法添加 window -- 此 window 类型的权限被拒绝
Unable to add window -- permission denied for this window type
我在服务中使用了 WindowManager,我一直试图从 this post 和其他 post 中找到解决方案,但我仍然收到此错误信息。
Unable to add window android.view.ViewRootImpl$W@b285d2e -- permission denied for this window type
我已经在我的 Manifest.xml 上使用 android.permission.SYSTEM_ALERT_WINDOW
<--- current 或者
android.permission.INTERNAL_SYSTEM_WINDOW
(此为系统app只能使用)
我的最低 sdk 是 API17.
错误信息:
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.access00(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.access00(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 and this复制代码
我复制这段代码有点旧,在项目下载中已经有 app-debug.apk(在清单中使用 android.permission.SYSTEM_ALERT_WINDOW
),我通常尝试它的 运行 但是当我使用那个代码到我的项目它给我错误信息。
我还必须使用什么权限?
这个问题自己解决了
更新
确保您已在清单中添加权限。
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
将它放在 onCreate 或任何你想要的 askPermission()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
askPermission()
}
方法:
@TargetApi(23)
public void askPermission() {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, SYSTEM_ALERT_WINDOW_PERMISSION);
}
我在服务中使用了 WindowManager,我一直试图从 this post 和其他 post 中找到解决方案,但我仍然收到此错误信息。
Unable to add window android.view.ViewRootImpl$W@b285d2e -- permission denied for this window type
我已经在我的 Manifest.xml 上使用 android.permission.SYSTEM_ALERT_WINDOW
<--- current 或者
android.permission.INTERNAL_SYSTEM_WINDOW
(此为系统app只能使用)
我的最低 sdk 是 API17.
错误信息:
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.access00(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.access00(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 and this复制代码
我复制这段代码有点旧,在项目下载中已经有 app-debug.apk(在清单中使用 android.permission.SYSTEM_ALERT_WINDOW
),我通常尝试它的 运行 但是当我使用那个代码到我的项目它给我错误信息。
我还必须使用什么权限?
这个问题自己解决了
更新
确保您已在清单中添加权限。
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
将它放在 onCreate 或任何你想要的 askPermission()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
askPermission()
}
方法:
@TargetApi(23)
public void askPermission() {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, SYSTEM_ALERT_WINDOW_PERMISSION);
}