【问题标题】:how to run an android foreground service in cordova?如何在科尔多瓦运行android前台服务?
【发布时间】:2021-11-12 09:43:35
【问题描述】:

首先,为了熟悉 Android 的后台任务和前台服务,我实现了一个原生 android 应用。在某些时候,我有一个工作的前台服务通知应用程序 - 很好。

        // native Android:
        Intent runningIntentService = new Intent(getApplicationContext(), ForegroundService.class);
        stopService(runningIntentService);
        startService(runningIntentService);   // the onCommandStart is being executed.

然后我将所有内容编译为cordova插件,但最终没有调用服务的onCommandStart方法。在科尔多瓦做什么才能启动服务?

        // Cordova:
        try {
           Intent runningIntentService = new Intent(cordova.getActivity().getApplicationContext(), ForegroundService.class);
           Log.d("test", "#1");  // gets logged
           cordova.getActivity().stopService(runningIntentService);
           Log.d("test", "#2");  // gets logged
           cordova.getActivity().startService(runningIntentService); // the onCommand is not being executed.
           Log.d("test", "#3");  // gets logged
        } catch (Exception e) {
           Log.e("test","Error: " + e.toString());  // nothing is catched.
        }

【问题讨论】:

    标签: android cordova cordova-android


    【解决方案1】:

    我的错误是在 plugin.xml 中为包的 android 服务名称设置了错误的路径:

            <service android:name=".ForegroundService"/>  <!-- wrong -->
            <service android:name="com.package.example.ForegroundService"/>  <!-- right -->
    

    【讨论】:

      猜你喜欢
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 2019-03-27
      • 2019-06-03
      • 1970-01-01
      相关资源
      最近更新 更多