https://facebook.github.io/react-native/docs/headless-js-android.html

  当app在 后台运行 时,我们可以使用RN服务来同时地刷新数据、推送或者播放音乐。

JS API

  在JS中注册一个后台任务(不能操作UI,一般是网络请求、定时器等)

AppRegistry.registerHeadlessTask('SomeTaskName', () => require('SomeTaskName'));

// SomeTaskName.js:
module.exports = async (taskData) => {
// do stuff
};

Java API

  定义一个服务类,继承HeadlessJsTaskService

public class MyTaskService extends HeadlessJsTaskService {

  @Override
protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
return new HeadlessJsTaskConfig(
"SomeTaskName",
Arguments.fromBundle(extras),
5000, // timeout for the task
false // optional: defines whether or not the task is allowed in foreground. Default is false
);
}
return null;
}
}

  在清单文件中声明这个服务

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

  在Java中开启这个服务

Intent service = new Intent(getApplicationContext(), MyTaskService.class);
Bundle bundle = new Bundle(); bundle.putString("foo", "bar");
service.putExtras(bundle); getApplicationContext().startService(service);

警告

  1. 默认情况下,当app前台运行时,开启这个服务会导致app崩溃。这是为了防止app在展示界面的同时运行大量的后台工作,可以通过第四个参数来改变这种行为(默认为false,不允许任务在前台运行)
  2. 如果从广播接收者中开启服务,必须保证在onReceive结束前调用HeadlessJsTaskService.aquireWakeLockNow()

例子

  这是一个可以响应网络连接变化的例子。首先在清单文件中配置广播接收者

<receiver android:name=".NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>

  广播接收者在onReceive中处理广播,可以在这里检查app是否运行在前台:

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<ActivityManager.RunningAppProcessInfo> 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());
}
}

实践

  测试发现必须要多加一个权限:

<uses-permission android:name="android.permission.WAKE_LOCK"/>

  对于以下代码:

module.exports = async (taskData) => {
await getData()
await getData()
await getData()
await getData()
await getData()
await getData()
}; function getData(){
return new Promise((resolve)=>{
setTimeout(()=>{
console.log("get data from server")
resolve(1)
},5000)
});
}

  当设置允许运行在前台时(allowedInForeground=true),前台或者后台开启服务都没问题,只是只要app进入后台,以上的代码就会被暂停,恢复前后后继续执行。

  当不允许前台运行时,前台开启服务则闪退,后台开启不会。后台开启后,仅仅执行了一个promise,就暂停了,恢复前台后剩余的代码继续执行,再次进入后台,则代码暂停,恢复前后剩余代码继续执行。

  以上运行的环境是夜神模拟器,还没到真机上测试。

RN服务的更多相关文章

  1. react native初步常见问题

    首先按照资料一步步搭建环境运行,然后成功了,很激动,可是,安卓就是没这么容易成功,还是太年轻了 could not get batchedbridge, make sure your bundle i ...

  2. React Native 之 main.jsbundle生成方法

    通过react-native init yooweiProject 生成的RN项目(版本基于0.57),目录结构如下 项目结构: 大家可以发现main.jsbundle 是红色的,不存在的,这个属于正 ...

  3. RN 数据持久化存储服务API

    一些数据信息需要存储在手机内存中,比如用户的登录名密码 token啥的,所以这就需要了来存这些信息 在RN中 采用了AsyncStorage是一个简单的.异步的.持久化的Key-Value存储系统,它 ...

  4. 使用dubbo分布式服务框架发布服务及消费服务

    什么是DUBBO DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案. 准备工作 安装zookeeper ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服 ...

  5. C# 自动Ping服务

    using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; u ...

  6. 5. 网络配置与FTP服务笔记

    IP地址: Ipv4        2*32       Ipv6 tcp      网络通讯协议 udp    用户数据报协议 常见网络端口: 20  21      ftp服务 文件共享 22   ...

  7. 客户端向服务端传送特殊字符解决方法(检测到有潜在危险的 Request.Form 值)

    当客户端向服务端传输特殊字符时报错,错误信息如下图:

  8. Nginx/Apache服务连接数梳理

    统计连接数,使用netstat命令或ss命令都可以1)统计连接数(80端口)[root@wang ~]# netstat -nat|grep -i "80"|wc -l872 或者 ...

  9. django中“url映射规则”和“服务端响应顺序”

    1.django搜索路径 使用 import 语句时,Python 所查找的系统目录清单.      查看方式:         import sys        print sys.path   ...

随机推荐

  1. C 语言实例 - 将字符串写入文件

    C 语言实例 - 将字符串写入文件 C 语言实例 C 语言实例 将字符串写入文件. 实例 #include <stdio.h> #include <stdlib.h> /* e ...

  2. 51Nod 1097 拼成最小的数(字符串的排序)

    #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> ...

  3. python Windows和Linux路径表示问题

    Windows下路径是用‘\\’表示也可以使用'/',但是Linux下路径都是‘/’表示. 因为python是跨平台的,有时候程序迁移会出现错误. 解决办法1 可全部使用‘/’表示 解决办法2 我们可 ...

  4. jQuery addClass() 源码解读

    addClass: function( value ) { var classes, elem, cur, clazz, j, i = 0, len = this.length, proceed = ...

  5. JavaScript Allongé 第一呷 :基础函数 (2)

    啊!我想要有一个参数 到现在为止,我们已经了解了没有参数的函数.只说我们的函数没有任何参数,甚至还没说参数是什么.大多数程序员非常熟悉参数,中学数学就讨论这个了.所以你知道他们是什么,而我也知道你知道 ...

  6. Nginx 开启目录浏览功能配置

    在server节点下添加 server { listen ; server_name default; #index index.php; # 目录浏览功能 autoindex on; # 显示文件大 ...

  7. Android 两个ArrayList找出相同元素及单个ArrayList删除元素

    //从一个ArrayList中删除重复元素 List<String> arrayList1 = new ArrayList<String>(); arrayList1.add( ...

  8. nuget用法

    Update-Package -reinstall -ProjectName Cardin.HeartCare.Service.ChatService

  9. IDEA 启用/禁用 Run Dashboard

    一.启用 方式一: 创建/打开一个SpringBoot项目[或者点击Run --> Edit Configurations 添加 Spring Boot 类型的项目配置:或者如图在红框处添加配置 ...

  10. python深浅拷贝问题

    Python中,对象的赋值,拷贝(深/浅拷贝)之间是有差异的,如果使用的时候不注意,就可能产生意外的结果. 下面本文就通过简单的例子介绍一下这些概念之间的差别. 一.对象赋值 又叫变量对对象的引用 l ...