From 4de9d64e620bfa81f5c00d7f24b00e600299c49b Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Mon, 24 Jul 2017 11:28:50 -0700 Subject: [PATCH] Improve Headless JS documentation Summary: Closes https://github.com/facebook/react-native/pull/15174 Differential Revision: D5481462 Pulled By: hramos fbshipit-source-id: 6bf293fabd30102f1eddf48f3afa4d3683fc577b --- docs/HeadlessJSAndroid.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/docs/HeadlessJSAndroid.md b/docs/HeadlessJSAndroid.md index 763f58c28c8..584e6d40c99 100644 --- a/docs/HeadlessJSAndroid.md +++ b/docs/HeadlessJSAndroid.md @@ -50,17 +50,29 @@ public class MyTaskService extends HeadlessJsTaskService { } ``` -Now, whenever you [start your service][0], e.g. as a periodic task or in response to some system event / broadcast, JS will spin up, run your task, then spin down. - -Remember to add the service to your `AndroidManifest` file: +Then add the service to your `AndroidManifest.xml` file: ``` ``` +Now, whenever you [start your service][0], e.g. as a periodic task or in response to some system event / broadcast, JS will spin up, run your task, then spin down. + +Example: + +```java +Intent service = new Intent(getApplicationContext(), MyTaskService.class); +Bundle bundle = new Bundle(); + +bundle.putString("foo", "bar"); +service.putExtras(bundle); + +getApplicationContext().startService(service); +``` + ## Caveats -* By default, your app will crash if you try to run a task while the app is in the foreground. This is to prevent developers from shooting themselves in the foot by doing a lot of work in a task and slowing the UI. There is a way around this. +* By default, your app will crash if you try to run a task while the app is in the foreground. This is to prevent developers from shooting themselves in the foot by doing a lot of work in a task and slowing the UI. You can pass a fourth `boolean` argument to control this behaviour. * If you start your service from a `BroadcastReceiver`, make sure to call `HeadlessJsTaskService.acquireWakelockNow()` before returning from `onReceive()`. [0]: https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)