如何解决为什么在Cordova中未调用“ BackgroundFetchHeadlessTask.java”?
我正在使用cordova-plugin-background-fetch执行后台进程,我甚至希望我的应用终止时运行后台任务。根据{{3}},为了使enableHeadless: true
工作,我需要编写Java代码,并将BackgroundFetchHeadlessTask.java
文件放置在应用程序中的任何位置。调试应用程序时,我看不到任何证据表明执行了BackgroundFetchHeadlessTask.java
。我的BackgroundFetchHeadlessTask.java
文件放在MyProjectFolder/src/android/BackgroundFetchHeadlessTask.java
中。
我也尝试过cordova-plugin-background-mode,但是当我终止我的应用程序时,它将停止后台进程。 cordova-plugin-background-fetch符合我的所有要求,所以这就是为什么我现在坚持使用它。
类似的问题在这里documentation提出,但不能解决我的问题。在这个问题中,@ Blue Ace在他的配置文件中有一个错误,该文件中的目标参数设置不正确。我的目标参数是根据here设置的。
我的代码:
JS代码
deviceReady: function() {
var BackgroundFetch = window.BackgroundFetch;
// Your background-fetch handler.
var fetchCallback = function(taskId) {
console.log('[js] BackgroundFetch event received: ',taskId);
// Required: Signal completion of your task to native code
// If you fail to do this,the OS can terminate your app
// or assign battery-blame for consuming too much background-time
BackgroundFetch.finish(taskId);
};
var failureCallback = function(error) {
console.log('- BackgroundFetch failed',error);
};
BackgroundFetch.configure(fetchCallback,failureCallback,{
minimumFetchInterval: 15,// <-- default is 15
enableHeadless: true,stopOnTerminate: false,startOnBoot: true,requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY
});
},
Java代码
package com.transistorsoft.cordova.backgroundfetch;
import android.content.Context;
import android.util.Log;
import com.transistorsoft.tsbackgroundfetch.BackgroundFetch;
/**
* Created by chris on 2018-01-22.
*/
public class BackgroundFetchHeadlessTask implements HeadlessTask {
@Override
public void onFetch(Context context,String taskId) {
Log.d(BackgroundFetch.TAG,"BackgroundFetchHeadlessTask onFetch -- DEFAULT IMPLEMENTATION");
BackgroundFetch.getInstance(context).finish(taskId);
}
}
config.xml
<platform name="android">
<allow-intent href="market:*" />
<resource-file src="src/android/BackgroundFetchHeadlessTask.java" target="app/src/main/java/com/transistorsoft/cordova/backgroundfetch/BackgroundFetchHeadlessTask.java" />
</platform>
这是我想要的:
2020-10-13 11:01:54.927 8566-8694/com.menu.menu D/TSBackgroundFetch: - BackgroundFetchHeadlessTask onFetch -- DEFAULT IMPLEMENTATION
这是我得到的:
2020-10-13 11:01:54.927 8566-8694/com.menu.menu D/TSBackgroundFetch: - configure
2020-10-13 11:01:54.928 8566-8694/com.menu.menu D/TSBackgroundFetch: - start
2020-10-13 11:01:54.929 8566-8694/com.menu.menu D/TSBackgroundFetch: - registerTask: cordova-background-fetch
2020-10-13 11:01:54.933 8566-8694/com.menu.menu D/TSBackgroundFetch: {
"taskId": "cordova-background-fetch","isFetchTask": true,"minimumFetchInterval": 15,"stopOnTerminate": false,"requiredNetworkType": 1,"requiresBatteryNotLow": false,"requiresCharging": false,"requiresDeviceIdle": false,"requiresStorageNotLow": false,"startOnBoot": true,"jobService": "com.transistorsoft.cordova.backgroundfetch.BackgroundFetchHeadlessTask","forceAlarmManager": false,"periodic": true,"delay": -1
}
2020-10-13 11:01:54.945 8566-8566/com.menu.menu D/TSBackgroundFetch: - Background Fetch event received
2020-10-13 11:01:55.009 8566-8694/com.menu.menu D/TSBackgroundFetch: - finish: cordova-background-fetch
2020-10-13 11:01:55.009 8566-8694/com.menu.menu D/TSBackgroundFetch: - jobFinished
2020-10-13 11:01:55.012 8566-8566/com.menu.menu I/chromium: [INFO:CONSOLE(61)] "[js] BackgroundFetch event received: cordova-background-fetch"
然后每15分钟:
2020-10-13 11:32:39.689 8566-8566/com.menu.menu D/TSBackgroundFetch: - Background Fetch event received
2020-10-13 11:32:39.693 8566-8566/com.menu.menu I/chromium: [INFO:CONSOLE(61)] "[js] BackgroundFetch event received: cordova-background-fetch"
2020-10-13 11:32:39.694 8566-8694/com.menu.menu D/TSBackgroundFetch: - finish: cordova-background-fetch
2020-10-13 11:32:39.694 8566-8694/com.menu.menu D/TSBackgroundFetch: - jobFinished
有没有解决此问题的方法,或者是否有任何其他替代cordova-plugin-background-fetch的方法?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。