Improve Headless JS documentation

Summary: Closes https://github.com/facebook/react-native/pull/15174

Differential Revision: D5481462

Pulled By: hramos

fbshipit-source-id: 6bf293fabd30102f1eddf48f3afa4d3683fc577b
This commit is contained in:
Satyajit Sahoo 2017-07-24 11:28:50 -07:00 committed by Facebook Github Bot
parent f39f21660d
commit 4de9d64e62
1 changed files with 16 additions and 4 deletions

View File

@ -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)