如何以编程方式从 BroadcastReceiver 启动 Flutter 应用程序?

How can I programmatically start a Flutter application from a BroadcastReceiver?

我实现一个BroadcastReceiver的目的是能够在来电或去电结束时启动关闭的Flutter应用程序。 Flutter 应用程序具有本机 Android 代码,以便配置允许在后台跟踪 phone 状态的服务。当 phone 状态改变时,BroadcastReceiver 工作并调用适当的函数。例如,onReceive方法在来电结束时调用onIncomingCallEnded 函数。然后通过 PackageManager 将此函数传递给 onReceive 方法的 Context 对象,该方法用于创建意图,如 this post 中所述。如果 Flutter 应用程序关闭,该意图应该会启动它。

    protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end)
    {
        Log.i("CallObserver", "We finished a call");

        Intent i = ctx.getPackageManager().getLaunchIntentForPackage("com.example.flutter_call_app_java");
        if(i != null) {
            Log.i("CallObserver","intent is not null");
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //Used to start an Activity outside of an Activity Context.
            ctx.startActivity(i);
        }
        else {
            Log.e("CallObserver","Couldn't start app");
        }
    }

调用onIncomingCallEnded函数时,log显示intent不为null,但是Flutter应用没有启动。 Logcat 中没有错误。我已经参考 James Gardiner as well as a separate post by nub and although their goals are related to mine, the solutions to their questions don't seem to work for my use case (I have tested their solutions). I have also implemented a remark by Ramakrishna Joshi 的 post 关于包名称在 AndroidManifest 中的位置,以检查用户设备上是否安装了包。我在 Android Studio Arctic Fox 中为 Android API 30 使用 Android 模拟器 | 2020.3.1 补丁 4,在 Windows 10 桌面上。

事实证明,David Wasser 的观察是正确的。使用 API 28 及以下版本时一切正常,但由于从后台启动活动的限制,API 29 及以上版本失败。通知的显示似乎是对我的用例的适当折衷。