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

时间:2019-08-31 07:36:12

标签: java android ionic-framework cordova-plugins

我正在做后台程序,目前在 ionic 中使用cordova's background fetch plugin。即使终止应用程序,我也想运行后台任务。它可以在iOS上运行,但documentation表示它还支持 android 。根据{{​​3}}的说法,为了使 select 'XYZ' as 'Sync By', select '2021-10-16' as 'Sync Date', (select ID, EmailAddress, StartDate, EndDate, from table EmailAddresses for json path) EmailAddresses, (select Id, CardNumber, ValidFrom, ValidTo, CVC from table Cards for json path) Cards for json path 能够正常工作,我需要编写一个Java代码,并将BackgroundFetchHeadlessTask.java文件放置在应用程序中的任何位置。创建BackgroundFetchHeadlessTask.java文件后,我仍然获得无头任务的默认实现。

我也尝试过documentation,但是当我终止我的应用程序时,它会停止后台进程。 cordova-plugin-background-mode符合我的所有要求,所以这就是为什么我现在坚持使用它。我目前将Java文件放置在:

  

myProjectFolder / www / src / android / BackgroundFetchHeadlessTask.java

这是我的代码:

JS代码

enableHeadless: true

Java代码

$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
    });
  }
});

config.xml

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();
    }
}

我想要得到的是:

<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

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

1 个答案:

答案 0 :(得分:1)

似乎正在执行插件的默认处理。 检查平台下的BackgroundFetchHeadlessTask.java是否已被覆盖。 如果未覆盖,则目标路径不正确。 target =“ src / com / transistorsoft / cordova / backgroundfetch / BackgroundFetchHeadlessTask.java”