HeadlessEvent¶
The event object delivered to a headless task handler when the app is running in a terminated (headless) state on Android.
With your app open in Android Studio, browse the app folder to find the MainActivity class.
Right-click the containing folder and click New > Java Class.

You MUST name the file BackgroundFetchHeadlessTask.

Add the following Java code, taking care to preserve your package declaration:
package com.your.package.name; // <-- DO NOT REPLACE THIS LINE
import android.content.Context;
import android.util.Log;
import com.transistorsoft.tsbackgroundfetch.BackgroundFetch;
import com.transistorsoft.tsbackgroundfetch.BGTask;
public class BackgroundFetchHeadlessTask {
public void onFetch(Context context, BGTask task) {
BackgroundFetch backgroundFetch = BackgroundFetch.getInstance(context);
String taskId = task.getTaskId();
boolean isTimeout = task.getTimedOut();
if (isTimeout) {
backgroundFetch.finish(taskId);
return;
}
Log.d("MyHeadlessTask", "onFetch: " + taskId);
// Do your work here...
backgroundFetch.finish(taskId);
}
}
taskId¶
taskId: string;
The identifier of the task that fired this headless event.
Pass this value to BackgroundFetch.finish when your work is complete.
timeout¶
timeout: boolean;
true when the OS signals that background running-time is nearly exhausted.
When a timeout event fires you must immediately call BackgroundFetch.finish and stop any in-progress work. Failure to do so may cause the OS to penalize your app.