使用 Cordova 插件创建 Android 启动服务

Create Android Startup Service with Cordova Plugin

是否可以创建一个 android 在 Cordova 插件中完成引导后自动启动的后台服务?

在我的具体示例中,我需要启动一个始终侦听蓝牙事件的服务(即使实际应用程序未启动)。

理论上,这只需要一个广播接收器来侦听 BOOT_COMPLETED 事件,然后启动一个广播接收器来侦听蓝牙事件。

但是,由于我需要使用 Cordova 应用程序,我无法访问上述解决方案所需的 AndroidManifest.xml 文件。

我的问题是:有没有一种方法可以从 Cordova 访问 AndroidManifest.xml 文件,或者有没有其他我没有想到的方法会给我同样的结果?

此致, 莱克斯

查看 plugin.xml 文件的文档:https://cordova.apache.org/docs/en/latest/plugin_ref/spec.html#config-file

您可以有一个名为 <config-file> 的部分,它可以添加到 AndroidManifest.xml。例如(来自文档):

<config-file target="AndroidManifest.xml" parent="/manifest/application">
    <activity android:name="com.foo.Foo" android:label="@string/app_name">
        <intent-filter>
        </intent-filter>
    </activity>
</config-file>

只需通过这种方式添加您的广播接收器。