gpt4 book ai didi

javascript - React Native Android 模块不起作用

转载 作者:行者123 更新时间:2023-11-28 18:13:38 24 4
gpt4 key购买 nike

我正在尝试在 React-Native 中运行后台服务。据我所知,我需要用 native Java 编写它并将其连接到 react native 代码。我制作了一个模块,它没有抛出任何错误,但它什么也没做。我尝试更改我的代码以显示一个简单的 toast ,但它甚至没有这样做。代码如下:

服务:

public class TestService extends Service {

double distance = 0.0;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

Toast.makeText(getApplicationContext(), "Service started...", Toast.LENGTH_SHORT).show();
final ReactContext reactContext = new ReactContext(getApplicationContext());
new Timer().scheduleAtFixedRate(new TimerTask(){
@Override
public void run(){
WritableMap params = Arguments.createMap();
distance+= 0.7;
params.putDouble("distance", distance);
sendEvent(reactContext, "updateDistance", params);
}
},0,1000);
return START_STICKY;
}

private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
reactContext.getJSModule(DeviceEventManagerModule
.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

模块:

public class ServiceModule extends ReactContextBaseJavaModule {
ReactContext reactContext;

public ServiceModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}

@ReactMethod
public void startTrackingService() {
Intent intent = new Intent(reactContext, TestService.class);
reactContext.startService(intent);
Toast.makeText(getReactApplicationContext(), "Starting service...", Toast.LENGTH_SHORT).show();
}

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

封装:

public class ServicePackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new ServiceModule(reactContext));
return modules;
}

@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}

在主应用程序中:

@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new ReactNativePushNotificationPackage(),
new ServicePackage()
);
}

然后在 react native 代码中 - ServiceModule.js:

import { NativeModules } from 'react-native';
module.exports = NativeModules.ServiceModule;

在 Tracking.js 中:

import ServiceModule from '../component/ServiceModule';
....
startTracking() {
console.warn("Trying to start serivce");
ServiceModule.startTrackingService;
}

componentWillMount() {
DeviceEventEmitter.addListener('updateDistance', function(e: Event) {
console.warn("got event");
this.updateDistance(e.distance);
});
}

updateDistance(newDistance) {
this.setState({distance: newDistance});
}

console.warn("Trying to start service"); 正在显示,因此肯定会调用 startTracking() 方法;

最佳答案

startTracking() {
console.warn("Trying to start serivce");
ServiceModule.startTrackingService();
}

关于javascript - React Native Android 模块不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41118367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com