使用Flutter在Android上实现在其他应用上层显示

huangapple go评论91阅读模式
英文:

Display Over Other Apps Using Flutter on Android

问题

可以在Android Flutter应用程序中启用“在其他应用程序上显示”吗?

通过与本机Android API进行桥接,可以在Flutter应用程序上启用此功能吗?能否有人详细说明一下这一点?

英文:

Is it possible to make enable the "Display Over Other Apps" on an Android Flutter app ?

Will bridging to the native Android API possible to enable this feature on Flutter apps? Can anyone elaborate on this please?

答案1

得分: 10

你所描述的功能由应用程序权限处理:

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

文档 指出该权限允许应用程序使用 WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY 类型创建窗口,在所有其他应用程序之上显示。

有点模糊的解释,不是吗?让我们弄清楚 TYPE_APPLICATION_OVERLAY 是什么意思。

应用程序覆盖窗口显示在所有活动窗口(类型介于 FIRST_APPLICATION_WINDOWLAST_APPLICATION_WINDOW 之间),但位于状态栏或输入法等关键系统窗口之下。

为什么我提到了这两行?

嗯,为了回答你的问题,我们需要了解如何在纯 Android 中实现这个功能。我们知道它是如何工作的。事实上,这种方法只适用于带有 UI 的 Android 组件 - 活动、服务和对话框(后两者需要额外的工作)。

那么在 Flutter 的背景下,这意味着什么呢?这意味着在干净的 Flutter 中没有设置可以启用这种行为,只有底层平台提供的工具。

在 Android 的上下文中,使用此权限将允许您在 Android UI 容器中使用此功能(需要通过权限管理屏幕明确请求)。

if(!Settings.canDrawOverlays(this)){
    // 请求权限
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
    Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, REQUEST_OVERLAY_PERMISSION);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_OVERLAY_PERMISSION) {
        if (Settings.canDrawOverlays(this)) {
            // 权限已授予...
        }else{
            // 权限未授予...
        }
    }
}

我不确定 Flutter 是否已经有类似的功能。

进一步探索后,发现 Flutter 在特殊的 Android 容器 FlutterActivityFlutterFragmentFlutterView 中呈现自身。

对于这三个容器,你可以通过相关权限来强制执行预期的行为。整个 Flutter UI 将具有将其绘制为覆盖的可能性。
但是,如果你只想要你的 Flutter UI 的某些部分以覆盖方式绘制,例如一些 Flutter 对话框或导航路线的部分 - 你将无法轻松实现(只能通过创建单独的 Android 本机 UI 容器,并在其中引导你的 Flutter UI - 截至 2023 年,多个(10 个或更多)Flutter UI 容器可能会导致性能问题)。

英文:

The feature you describe is handled by the application permission

&lt;uses-permission android:name=&quot;android.permission.SYSTEM_ALERT_WINDOW&quot; /&gt;

Documentation states that this permission

> Allows an app to create windows using the type WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, shown on top of all other apps.

A bit vague explanation isn't it? Lets find out what does TYPE_APPLICATION_OVERLAY means.

> Application overlay windows are displayed above all activity windows (types between FIRST_APPLICATION_WINDOW and LAST_APPLICATION_WINDOW) but below critical system windows like the status bar or IME.

Why have I mentioned these two lines?

Well, to answer your question, we need to understand how to achieve the thing in plain Android. And we do know how it works. The thing is, this method only works for Android components with UI - activities, services, and dialogs(the latter two will require some extra work).

What does this mean in connection with Flutter? This means that there are no settings in clean Flutter to enable such behavior, only the tools the underlying platform offers.

In the context of Android using this permission will allow you to use this feature for Android UI containers(it needs to be explicitly requested through a permission management screen like this)

if(!Settings.canDrawOverlays(this)){
    //Ask for setting 
     Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
     Uri.parse(&quot;package:&quot; + getPackageName()));
     startActivityForResult(intent, REQUEST_OVERLAY_PERMISSION);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_OVERLAY_PERMISSION) {
        if (Settings.canDrawOverlays(this)) {
            // permission granted...
        }else{
            // permission not granted...
        }
    }
}

And I am not sure there is a Flutter analog yet.

Exploring further, it turns out that the Flutter renders itself in special android containers FlutterActivity, FlutterFragment, and FlutterView.

For these three, you are able to force intended behavior with relevant permission. The whole flutter UI will have the possibility to be drawn as an overlay.
But if you want only some parts of your flutter UI to be drawn as overlays, for example, some flutter dialogs or parts of the navigation route - you will not be able to achieve it that easily(only by creating a separate Android native UI containers and channeling your flutter UI there - as of 2023 multiple(10 and more) flutter UI containers may cause performance issues problems)

答案2

得分: 1

已经有一个可用的插件,声称可以在所有其他应用程序上方显示类似Truecaller的覆盖窗口,并附带回调事件,

请查看插件system_alert_window的链接:https://pub.flutter-io.cn/packages/system_alert_window

system_alert_window

一个用于显示类似Truecaller覆盖窗口的Flutter插件,可在所有其他应用程序上方显示,同时具有回调事件。在Android Go或Android 11及更高版本中,此插件会显示通知气泡;在其他Android版本中,它会显示一个覆盖窗口。

英文:

There already is a plugin available that claims to "show Truecaller like overlay window over all other apps along with callback events",

Please checkout the plugin system_alert_window URL: https://pub.flutter-io.cn/packages/system_alert_window

> system_alert_window
>
> A flutter plugin to show Truecaller like overlay window, over all
> other apps along with callback events. Android Go or Android 11 &
> above, this plugin shows notification bubble, in other android
> versions, it shows an overlay window.

huangapple
  • 本文由 发表于 2020年10月5日 22:33:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64210724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定