我希望使我的应用始终位于所有其他窗口之上,并使点击次数传递到下面的其他窗口。到目前为止,正在使用以下代码(从stackoverflow的各个位置获取):
在onCreate下,我致电:
checkDrawOverlayPermission();
下面是checkDrawOverlayPermission()的代码。旨在获取用户许可以启动应用程序作为叠加层的意图:
public void checkDrawOverlayPermission() {
/** check if we already have permission to draw over other apps */
if (!Settings.canDrawOverlays(this)) {
/** if not construct intent to request permission */
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
/** request permission via start activity for result */
startActivityForResult(intent, REQUEST_CODE);
}
}
onActivityResult()部分如下:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
...
case REQUEST_CODE:
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
0 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
params.gravity = Gravity.RIGHT | Gravity.TOP;
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(imagePane, params);
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
清单文件具有以下行:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
作为视图添加到叠加层的imagePane(ImageView)在activity_main.xml中设置了以下属性:
android:background="@android:color/transparent"
android:scaleType="centerInside"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
使用上面的代码,该应用程序在电话主屏幕上显示为透明窗口。但是,此应用下方的窗口未收到点击或触摸。我尝试运行
imagePane.setEnabled(false);
它确实禁用了对imagePane的点击,但是此应用下方的窗口仍然没有收到这些点击。知道如何让应用下方的Windows接收点击吗?