Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headless Setup #69

Closed
nsmith1024 opened this issue May 30, 2019 · 15 comments
Closed

Headless Setup #69

nsmith1024 opened this issue May 30, 2019 · 15 comments

Comments

@nsmith1024
Copy link

Your setup instructions in github says.....

Headless Mechanism with enableHeadless: true

Create either Application.kt or Application.java in the same directory as MainActivity

I prefer using Application.java , but I dont see MainActivity anywhere in my flutter project tree, i just see a file called GeneratedPluginRegistrant.java should i place Application.java in that same folder?

Your Environment

  • Plugin version:
  • Platform: iOS or Android
  • OS version:
  • Device manufacturer / model:
  • Flutter info (flutter doctor):
  • Plugin config:
PASTE_YOUR_CODE_HERE

Expected Behavior

Actual Behavior

Steps to Reproduce

Context

Debug logs

Logs
PASTE_YOUR_LOGS_HERE
@nsmith1024
Copy link
Author

Next the instructions says to add this to my manifest, but mine already has

<application
    tools:replace="android:label"
    android:name=".App"

Should i replace the name .App with .Application instead?

@christocracy
Copy link
Member

Have you managed to launch the plugin yet?

@nsmith1024
Copy link
Author

nsmith1024 commented May 30, 2019

i launched it in your example project, now im trying to integrate it into our project
following the instructions on https://github.com/transistorsoft/flutter_background_geolocation/blob/master/docs/INSTALL-ANDROID.md

@christocracy
Copy link
Member

I suggest you keep it simple in the beginning. Implementing the Headless task with enableHeadless: true is an advanced topic best left for when you’re more experienced with the plugin.

@nsmith1024
Copy link
Author

ok, where do i disable enableHeadless:true

@christocracy
Copy link
Member

What do the docs tell you about Config.enableHeadless?

@nsmith1024
Copy link
Author

i see it now, nice!

@christocracy
Copy link
Member

As for your "missing MainActivity.java, it should exist in the Android directory like so:

You were using an old version of Flutter, so perhaps your app-structure is likewise old. If so, I suggest you generate a new app with flutter create and migrate your /lib directory (ie: your application code) and dependencies from pubspec.yaml into the newly generated app.

@christocracy
Copy link
Member

And for the AndroidManifest.xml, for the headless configuration, If I create a custom Application extension class and call it Banana.java, my AndroidManifest.xml would be configured like so:

<application
    tools:replace="android:label"
    android:name=".Banana"

@nsmith1024
Copy link
Author

Thanks for your help, i never heard of headless mode before, i have so many questions about it. I have to research it more i guess....

Your description says headless is only an android thing (IOS dont have it). In android, when the app is terminated and you have headless enabled and there is an event, the registered headless function will be called. In IOS the whole app will wake up to process the event, while in android only that headless function that i register will exist.

So im wondering.... when this registered headless function is called to process the event, will the rest of the app data exist and can be access by the headless function? For example will the data for the user that was logged in when the app went to sleep (or terminated by the OS) exist when the headless function is called, or is the headless function just hangout there is space by itself?

What if i dont use the android headless feature, will the whole android app wake up (like the IOS app) to process the event instead?

I guess i have to read up more on this headless thing.....

Thanks anyway!

@christocracy
Copy link
Member

christocracy commented May 30, 2019

when this registered headless function is called to process the event, will the rest of the app data exist and can be access by the headless function?

In Headless context, your Flutter app, booted in main() {} with runApp does not get launched. You have access to any libraries you import into main.dart, SharedPreferences is a good place to share "the data for the user that was logged in" between your Flutter app and the headless context.

main.dart

// Headless task
void backgroundGeolocationHeadlessTask() async {
  print('[BackgroundGeolocation] headless task');
}

void main() {  // <-- in Headless context, main() does NOT run.
  runApp(new MyApp());  // <-- Your app does not launch in the headless context.

  // Register to receive BackgroundGeolocation events after app is terminated.
  // Requires {stopOnTerminate: false, enableHeadless: true}
  bg.BackgroundGeolocation.registerHeadlessTask(backgroundGeolocationHeadlessTask);
}

@nsmith1024
Copy link
Author

Thanks, but what if i dont use the headless feature, will the whole android app wake up instead of just the registered headless function?

@christocracy
Copy link
Member

will the whole android app wake up instead of just the registered headless function?

BackgroundGeolocation runs an android foreground-service. It remains running in spite of app termination (assuming you've configured stopOnTerminate: false). It's already running "headless", regardless of whether you've subscribed a headless-task. BackgroundGeolocation contains its own native Android HTTP service, capable of uploading recorded locations to your server without any intervention by you (see API docs for "Http Guide"). The plugin is completely capable of tracking a device forever and uploading each location without the need for a headless-task, regardless if the app is terminated or device rebooted.

Assuming by "whole app" you mean your UI you've declared in your Flutter app, no. That will never be launched automatically after the user or OS terminates your Android app.

Someone might implement a headless-task to respond to geofence events for example. Maybe they want to display a notification if the device is near some shop offering a deal. Their headless-task would respond to geofence events and interact with some 3rd-party notification library.

You don't need to implement a headless-task if you have nothing to do in the terminated context. The plugin is going to be tracking the location no matter what.

@nsmith1024
Copy link
Author

Ok, sounds perfect. So i dont need headless. When the user terminates the app, everything is dead if StopOnTerminate:true (which is what we want). When the user terminates the app, we want everything to stop, no background tasks or event processing.

But if the user switches to another app without terminating our app, or if our app goes to background mode because the screen went off, everything will still work once the user didnt manually terminate the app.

@christocracy
Copy link
Member

enableHeadless: true requires stopOnTerminate: false. If you're going to configure stopOnTerminate: true, you have no business even thinking about Headless Mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants