为什么 'BackgroundFetchHeadlessTask.java' 在 ionic 中不起作用?

Why 'BackgroundFetchHeadlessTask.java' does not work in ionic?

我正在处理后台进程,目前正在使用 cordova's background fetch plugin in ionic. I want to run my background task even when I terminate my app. It works in iOS but the documentation says it also supports android. According to the documentation,为了使 enableHeadless: true 正常工作,我需要编写 java 代码并可以放置 BackgroundFetchHeadlessTask.java 文件在我的应用程序中的任何位置。创建 BackgroundFetchHeadlessTask.java 文件后,我仍然获得无头任务的默认实现。

我也尝试过 cordova-plugin-background-mode but it stops the background process when I terminate my app. cordova-plugin-background-fetch 满足我的所有要求,所以这就是我现在坚持使用它的原因。我目前已将我的 java 文件放在:

myProjectFolder/www/src/android/BackgroundFetchHeadlessTask.java

这是我的代码:

JS代码

$ionicPlatform.ready(function(){
  var BackgroundFetch = window.BackgroundFetch;
  function background() {
    var fetchCallback = function() {
      console.log('[js] BackgroundFetch event received');
      BackgroundFetch.finish();
    };
    var failureCallback = function(error) {
      console.log('- BackgroundFetch failed', error);
    };
    BackgroundFetch.configure(fetchCallback, failureCallback, {
      minimumFetchInterval: 15, // <-- default is 15
      stopOnTerminate: false,
      enableHeadless: true
    });
  }
});

Java代码

package com.transistorsoft.cordova.backgroundfetch;
import android.content.Context;
import com.transistorsoft.tsbackgroundfetch.BackgroundFetch;
import android.util.Log;

public class BackgroundFetchHeadlessTask implements HeadlessTask {
    @Override
    public void onFetch(Context context) {
        Log.d(BackgroundFetch.TAG, "My BackgroundFetchHeadlessTask:  onFetch");
        // Perform your work here.
        Log.d(BackgroundFetch.TAG, "Yes, I am running");

        // Just as in Javascript callback, you must signal #finish
        BackgroundFetch.getInstance(context).finish();
    }
}

config.xml

<platform name="android">
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
            <application android:networkSecurityConfig="@xml/network_security_config" />
        </edit-config>
        <resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />
        <allow-intent href="market:*" />
        <icon density="ldpi" src="resources/android/icon/drawable-ldpi-icon.png" />
        <icon density="mdpi" src="resources/android/icon/drawable-mdpi-icon.png" />
        <icon density="hdpi" src="resources/android/icon/drawable-hdpi-icon.png" />
        <icon density="xhdpi" src="resources/android/icon/drawable-xhdpi-icon.png" />
        <icon density="xxhdpi" src="resources/android/icon/drawable-xxhdpi-icon.png" />
        <icon density="xxxhdpi" src="resources/android/icon/drawable-xxxhdpi-icon.png" />
        <splash density="land-ldpi" src="resources/android/splash/drawable-land-ldpi-screen.png" />
        <splash density="land-mdpi" src="resources/android/splash/drawable-land-mdpi-screen.png" />
        <splash density="land-hdpi" src="resources/android/splash/drawable-land-hdpi-screen.png" />
        <splash density="land-xhdpi" src="resources/android/splash/drawable-land-xhdpi-screen.png" />
        <splash density="land-xxhdpi" src="resources/android/splash/drawable-land-xxhdpi-screen.png" />
        <splash density="land-xxxhdpi" src="resources/android/splash/drawable-land-xxxhdpi-screen.png" />
        <splash density="port-ldpi" src="resources/android/splash/drawable-port-ldpi-screen.png" />
        <splash density="port-mdpi" src="resources/android/splash/drawable-port-mdpi-screen.png" />
        <splash density="port-hdpi" src="resources/android/splash/drawable-port-hdpi-screen.png" />
        <splash density="port-xhdpi" src="resources/android/splash/drawable-port-xhdpi-screen.png" />
        <splash density="port-xxhdpi" src="resources/android/splash/drawable-port-xxhdpi-screen.png" />
        <splash density="port-xxxhdpi" src="resources/android/splash/drawable-port-xxxhdpi-screen.png" />
        <resource-file src="www/src/android/BackgroundFetchHeadlessTask.java" target="src/com/transistorsoft/cordova/backgroundfetch/BackgroundFetchHeadlessTask.java" />
</platform>

我想得到的是:

08-31 12:13:07.722  1169  1169 D TSBackgroundFetch: - My BackgroundFetchHeadlessTask:  onFetch
08-31 12:13:07.741  1169  1169 D TSBackgroundFetch: - Yes, I am running

我得到的是:

08-31 11:43:07.654 27610 27610 D TSBackgroundFetch: - Background Fetch event received
08-31 11:43:07.665 27610 27610 D TSBackgroundFetch: - finish
08-31 11:43:07.665 27610 27610 D TSBackgroundFetch: - jobFinished
08-31 11:43:07.689 27610 27610 D TSBackgroundFetch: HeadlessJobService onStartJob
08-31 11:43:07.690 27610 27610 D TSBackgroundFetch: BackgroundFetchHeadlessTask onFetch -- DEFAULT IMPLEMENTATION
08-31 11:43:07.690 27610 27610 D TSBackgroundFetch: - finish
08-31 11:43:07.690 27610 27610 D TSBackgroundFetch: HeadlessJobService jobFinished
08-31 11:58:07.452 27610 27610 D TSBackgroundFetch: - Background Fetch event received
08-31 11:58:07.452 27610 27610 D TSBackgroundFetch: - finish
08-31 11:58:07.452 27610 27610 D TSBackgroundFetch: - jobFinished
08-31 11:58:07.482 27610 27610 D TSBackgroundFetch: HeadlessJobService onStartJob
08-31 11:58:07.482 27610 27610 D TSBackgroundFetch: BackgroundFetchHeadlessTask onFetch -- DEFAULT IMPLEMENTATION
08-31 11:58:07.482 27610 27610 D TSBackgroundFetch: - finish

是否有任何解决此问题的方法或 cordova-plugin-background-fetch 的任何其他替代方法?

好像是在执行插件的默认处理。 检查平台下的BackgroundFetchHeadlessTask.java是否被覆盖。 如果没有覆盖,则目标路径不正确。 目标="src/com/transistorsoft/cordova/backgroundfetch/BackgroundFetchHeadlessTask.java"