Permalink
Browse files

Improve Headless JS documentation

Summary: Closes #15174

Differential Revision: D5481462

Pulled By: hramos

fbshipit-source-id: 6bf293fabd30102f1eddf48f3afa4d3683fc577b
  • Loading branch information...
satya164 authored and facebook-github-bot committed Jul 24, 2017
1 parent f39f216 commit 4de9d64e620bfa81f5c00d7f24b00e600299c49b
Showing with 16 additions and 4 deletions.
  1. +16 −4 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:

```
<service android:name="com.example.MyTaskService" />
```

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)

0 comments on commit 4de9d64

Please sign in to comment.