为什么'BackgroundFetchHeadlessTask.java'在ionic中不起作用? [英] Why 'BackgroundFetchHeadlessTask.java' does not work in ionic?

查看:86
本文介绍了为什么'BackgroundFetchHeadlessTask.java'在ionic中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事后台进程,目前正在使用 cordova的后台获取插件 ionic .即使终止应用程序,我也想运行后台任务.它可以在iOS上运行,但文档表示它还支持 android .根据文档,以使 enableHeadless:true 才能正常工作,我需要编写一个Java代码,并将BackgroundFetchHeadlessTask.java文件放置在我的应用程序中的任何位置.创建BackgroundFetchHeadlessTask.java文件后,我仍然获得无头任务的默认实现.

I'm working on background processes and currently using 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, in order to make enableHeadless: true to work I need to write a java code and can place the BackgroundFetchHeadlessTask.java file anywhere in my app. After creating BackgroundFetchHeadlessTask.java file I'm still getting the default implementation for headless task.

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

I have also tried cordova-plugin-background-mode but it stops the background process when I terminate my app. cordova-plugin-background-fetch meets my all requirements so that's why I'm sticking with it for now. I have currently placed my java file in:

myProjectFolder/www/src/android/BackgroundFetchHeadlessTask.java

myProjectFolder/www/src/android/BackgroundFetchHeadlessTask.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
    });
  }
});

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的方法?

Is there any solution to this problem or any other alternative to cordova-plugin-background-fetch?

推荐答案

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

It seems that the default processing of the plug-in is being executed. Check if BackgroundFetchHeadlessTask.java under the platform has been overwritten. If it is not overwritten, the target path is incorrect. target="src/com/transistorsoft/cordova/backgroundfetch/BackgroundFetchHeadlessTask.java"

这篇关于为什么'BackgroundFetchHeadlessTask.java'在ionic中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆