【问题标题】:Why 'BackgroundFetchHeadlessTask.java' does not work in ionic?为什么'BackgroundFetchHeadlessTask.java'在离子中不起作用?
【发布时间】:2020-01-04 06:15:54
【问题描述】:

我正在处理后台进程,目前在 ionic 中使用cordova's background fetch plugin。即使我终止我的应用程序,我也想运行我的后台任务。它适用于 iOS,但documentation 表示它也支持 android。根据documentation,为了使enableHeadless: true 工作,我需要编写一个java 代码,并且可以将BackgroundFetchHeadlessTask.java 文件放置在我的应用程序的任何位置。创建 BackgroundFetchHeadlessTask.java 文件后,我仍然获得了无头任务的默认实现。

我也尝试过cordova-plugin-background-mode,但是当我终止我的应用程序时它会停止后台进程。 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 的方法?

【问题讨论】:

    标签: java android ionic-framework cordova-plugins


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复。我已经解决了这个问题,但忘记了我在这里发布了我的问题,因为我不是活跃用户。你是对的。我刚刚在 config.xml 中更改了“BackgroundFetchHeadlessTask.java”的目标,我的问题在那里解决了。
    • @BlueAce。我是同样的问题,我修复了路径。但是我没有得到 TSBackgroundFetch: - My BackgroundFetchHeadlessTask: onFetch 。请给我们完整的代码。谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 2023-02-15
    • 2021-12-17
    • 2015-02-23
    相关资源
    最近更新 更多