From 8b9df81c27944211e876e185e1fc008b005be0fd Mon Sep 17 00:00:00 2001 From: Pavol Fulop Date: Thu, 31 Aug 2017 11:13:23 -0700 Subject: [PATCH] Provide example of android side implementation of HeadlessJS Summary: Thanks for submitting a PR! Please read these instructions carefully: - [x] Explain the **motivation** for making this change. - [ ] Provide a **test plan** demonstrating that the code is solid. - [ ] Match the **code formatting** of the rest of the codebase. - [ ] Target the `master` branch, NOT a "stable" branch. The existing documentation on this topic doesn't provide enough of detail on where to start a headlessJS task. Just docs change This PR extends HeadlessJS documentation to show an example on how to start HeadlessJsTaskService. Sample code in example reacts to connectivity change using custom broadcast receiver logic that bundles additional information and starts the aforementioned service. Closes https://github.com/facebook/react-native/pull/13957 Differential Revision: D5746809 Pulled By: hramos fbshipit-source-id: 3eeea7d4a71382acf4b6f7ad1b99d20e2745c558 --- docs/HeadlessJSAndroid.md | 70 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/docs/HeadlessJSAndroid.md b/docs/HeadlessJSAndroid.md index e55f0f279..c0fe64999 100644 --- a/docs/HeadlessJSAndroid.md +++ b/docs/HeadlessJSAndroid.md @@ -75,4 +75,74 @@ getApplicationContext().startService(service); * 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()`. +## Example Usage + +Service can be started from Java API. First you need to decide when the service should be started and implement your solution accordingly. Here is a simple example that reacts to network connection change. + +Following lines shows part of Android manifest file for registering broadcast receiver. +```xml + + + + + +``` + +Broadcast receiver then handles intent that was broadcasted in onReceive function. This is a great place to check whether your app is on foreground or not. If app is not on foreground we can prepare our intent to be started, with no information or additional information bundled using putExta (keep in mind bundle can handle only parcelable values). In the end service is started and wakelock is acquired. + +```java +public class NetworkChangeReceiver extends BroadcastReceiver { + + @Override + public void onReceive(final Context context, final Intent intent) { + /** + This part will be called everytime network connection is changed + e.g. Connected -> Not Connected + **/ + if (!isAppOnForeground((context))) { + /** + We will start our service and send extra info about + network connections + **/ + boolean hasInternet = isNetworkAvailable(context); + Intent serviceIntent = new Intent(context, MyTaskService.class); + serviceIntent.putExtra("hasInternet", hasInternet); + context.startService(serviceIntent); + HeadlessJsTaskService.acquireWakeLockNow(context); + } + } + + private boolean isAppOnForeground(Context context) { + /** + We need to check if app is in foreground otherwise the app will crash. + http://stackoverflow.com/questions/8489993/check-android-application-is-in-foreground-or-not + **/ + ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); + List appProcesses = + activityManager.getRunningAppProcesses(); + if (appProcesses == null) { + return false; + } + final String packageName = context.getPackageName(); + for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { + if (appProcess.importance == + ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && + appProcess.processName.equals(packageName)) { + return true; + } + } + return false; + } + + public static boolean isNetworkAvailable(Context context) { + ConnectivityManager cm = (ConnectivityManager) + context.getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo netInfo = cm.getActiveNetworkInfo(); + return (netInfo != null && netInfo.isConnected()); + } + + +} +``` + [0]: https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)