时,Android 12 会阻止触摸-6ren"> 时,Android 12 会阻止触摸-我正在使用 显示一个覆盖在其他应用程序上但 android 12 阻止触摸的弹出窗口。 根据文档,它会阻止触摸事件 我怎样才能允许触摸 android 12 。 这是 android 12 行为变化触-6ren">
gpt4 book ai didi

使用 时,Android 12 会阻止触摸

转载 作者:行者123 更新时间:2023-12-04 23:50:16 27 4
gpt4 key购买 nike

我正在使用 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />显示一个覆盖在其他应用程序上但 android 12 阻止触摸的弹出窗口。
根据文档,它会阻止触摸事件
我怎样才能允许触摸 android 12 。
这是 android 12 行为变化触摸事件的链接
https://developer.android.com/about/versions/12/behavior-changes-all#untrusted-touch-events

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.input.InputManager;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;


public class FloatWidgetService extends Service {

private WindowManager mWindowManager;
private View mFloatingWidget;

public FloatWidgetService() {
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
final WindowManager.LayoutParams params;
mFloatingWidget = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget, null);
mFloatingWidget.setFilterTouchesWhenObscured(true);
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.START;
params.x = 0;
params.y = 100;
try {
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mFloatingWidget, params);


mFloatingWidget.findViewById(R.id.root_container).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:
int Xdiff = (int) (event.getRawX() - initialTouchX);
int Ydiff = (int) (event.getRawY() - initialTouchY);
if (Xdiff < 10 && Ydiff < 10) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
stopSelf();
}
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
mWindowManager.updateViewLayout(mFloatingWidget, params);
return true;
}
return false;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onDestroy() {
super.onDestroy();
if (mFloatingWidget != null) mWindowManager.removeView(mFloatingWidget);
}

}

最佳答案

尝试以下窗口类型:

val windowType: Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
}
TYPE_APPLICATION_OVERLAY .
这是 Kotlin,但您应该能够轻松转换为 Java。

关于使用 <uses-permission android :name ="android.permission.SYSTEM_ALERT_WINDOW"/> 时,Android 12 会阻止触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72110210/

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