在从无障碍服务中添加视图时使用TYPE_ACCESSIBILITY_OVERLAY似乎可以解决问题。我做了一个快速测试,甚至在设置菜单中也显示了覆盖窗口。覆盖窗口也接收到触摸事件。这在没有清单中的许可的情况下也有效,也没有用户交互地设置“在其他应用程序上显示”权限。我使用目标 SDK 29 进行了测试。type
WindowManager.LayoutParams
SYSTEM_ALERT_WINDOW
抱歉,我无法回答您关于适用哪些具体限制的第三个问题。
编辑:通过查看 Google here的旧教程,这里有一个简短的示例:
GlobalActionBarService.java
public class GlobalActionBarService extends AccessibilityService {
FrameLayout mLayout;
@Override
protected void onServiceConnected() {
// Create an overlay and display the action bar
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
mLayout = new FrameLayout(this);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
lp.format = PixelFormat.TRANSLUCENT;
lp.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.TOP;
LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.action_bar, mLayout);
wm.addView(mLayout, lp);
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
}
@Override
public void onInterrupt() {
}
}
显现
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
package="com.lb.myapplication">
<application
android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true"
android:theme="@style/Theme.MyApplication" tools:ignore="AllowBackup">
<activity
android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".GlobalActionBarService" android:exported="false"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice" android:resource="@xml/global_action_bar_service" />
</service>
</application>
</manifest>
global_action_bar_service.xml
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityFeedbackType="feedbackGeneric" android:accessibilityFlags="flagDefault"
android:canPerformGestures="true" android:canRetrieveWindowContent="true" />
action_bar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button
android:id="@+id/power" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/power" />
<Button
android:id="@+id/volume_up" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/volume" />
<Button
android:id="@+id/scroll" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/scroll" />
<Button
android:id="@+id/swipe" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/swipe" />
</LinearLayout>