gpt4 book ai didi

棉花糖或更高版本的窗口类型 2010 的 Android 权限被拒绝

转载 作者:太空狗 更新时间:2023-10-29 13:13:02 26 4
gpt4 key购买 nike

我正在尝试让 Chathead 类型在我的应用程序上过度绘制通过 Android 服务,如下图所示。

enter image description here

此聊天头应用适用于 Android 版本 5 或更低版本(kitkat、lollipop 等)。但我在 Marshmallow 和更高版本中尝试它,然后出现此错误。

 android.view.WindowManager$BadTokenException: Unable to add window
android.view.ViewRootImpl$W@48f5767 -- permission denied for window type 2010

enter image description here

聊天头像代码

注意:我是从 Android 服务调用这个函数

权限列表:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

private WindowManager windowManager;
private ImageView chatHead;
WindowManager.LayoutParams params;

@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.settingsicon);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);

params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(chatHead, params);

chatHead.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(chatHead, params);
return true;
}
return false;
}
}
}

如何在 Mashmallow 或更高版本的 android 上解决此错误?

最佳答案

在显示聊天头像前调用此方法请求权限:

 public void addOverlay() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
askedForOverlayPermission = true;
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, OVERLAY_PERMISSION_CODE);
}
}
}

在权限结果之后,如果用户允许权限,那么只有您可以显示聊天头像,如下所示:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == OVERLAY_PERMISSION_CODE) {
askedForOverlayPermission = false;
if (Settings.canDrawOverlays(this)) {
// SYSTEM_ALERT_WINDOW permission not granted...
//Toast.makeText(MyProtector.getContext(), "ACTION_MANAGE_OVERLAY_PERMISSION Permission Granted", Toast.LENGTH_SHORT).show();
Intent serviceIntent = new Intent(Homepage.this, ChatHeadService.class);
serviceIntent.putExtra("removeUserId", friendId);
startService(serviceIntent);

} else {
Toast.makeText(MyProtector.getContext(), "ACTION_MANAGE_OVERLAY_PERMISSION Permission Denied", Toast.LENGTH_SHORT).show();
}
}

如果对您有帮助,请告诉我...祝您好运。

关于棉花糖或更高版本的窗口类型 2010 的 Android 权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37982167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com