I am using <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
to show a pop up which overlay over other apps but android 12 is blocking the touches .
As per the documentation it blocks the touch events
how can i allow touches on android 12 .
Here is the link for android 12 behavioural changes touch events
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);
}
}
2
Answers
Try the following for the window type:
See TYPE_APPLICATION_OVERLAY.
This is Kotlin, but you should be able to convert to Java easily.
I don’t think that’s possible anymore. As stated in the docs, applications with window type:
TYPE_APPLICATION_OVERLAY
Aren’t trusted and therefore won’t be excepted from this protection.
How ever, they do write this:
So you can try removing:
and instead set a real color, but with 0 alpha, such as (#00XXXXXX)
might work but that’s just a guess