React-Native后台进程(android)处理? [英] React-native background process(android) handling?

查看:323
本文介绍了React-Native后台进程(android)处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在后台进程中或在Android平台上终止应用程序时定期console.log()?

How to console.log() something periodically in the background process or when the app is terminated on android platform?

推荐答案

,您需要创建自定义Java模块,该模块将在后台运行.例如:

you need to create custom Java module which will run in the background. For example:

@ReactMethod
public void startTimeTasks(Integer delay1, Integer delay2) {
    if (timer != null) {
        timer.cancel();
        timer.purge();
    }
    timer = new Timer();

timer.schedule(new TimeTask(), delay1);
timer.schedule(new TimeTask(), delay2);

}

@ReactMethod
public void cancelTimeTasks() {
    if (timer != null) {
        timer.cancel();
    }
}

@Override
public String getName() {
    return "MyCustomModule";
}

class TimeTask extends TimerTask {
    public void run() {
        //do something
    }
}

然后在JS中调用:

//run background task after 300000 and 240000 milliseconds
NativeModules.MyCustomModule.startTimeTasks(300000, 240000);
//stop this background task
NativeModules.MyCustomModule.cancelTimeTasks();

这是我的情况,但基于它可以做任何事情

it is my case but based on it can do anything

这篇关于React-Native后台进程(android)处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆