gpt4 book ai didi

java - 如何在 React Native 中从 Java 代码访问 ReactContext?

转载 作者:行者123 更新时间:2023-11-30 10:46:08 26 4
gpt4 key购买 nike

我试图创建一个应用程序,它会使用 BroadcastReciver 捕获手机的来电。从我的 BroadcastReciver 我想使用 this 将数字作为事件发送到我的 JS 文件方法。

我已经检查过我的 Java 代码是否正常工作并且正在捕获调用和号码,但是我的应用程序因提到 react 上下文为空的错误而崩溃。我猜这是因为当来自 android 系统的事件被捕获并且新实例没有 ReactContext 时, list (或其他东西)正在创建类的新实例。有什么方法可以从 Java 代码访问 ReactContext 或通过 list 将 ReactContext 发送到 BroadcastReciver?

这是我的广播接收者:

package com.bridgetest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.JavaScriptModule;


import android.widget.Toast;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;

import javax.annotation.Nullable;

/**
* Created by Erik on 2016-04-06.
*/
public class BroadcastReceiverCustom extends BroadcastReceiver {

ReactContext reactContext;

public BroadcastReceiverCustom (){
}

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
// This code will execute when the phone has an incoming call

// get the phone number
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();
sendCallEvent(incomingNumber);

} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)
|| intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// This code will execute when the call is disconnected
Toast.makeText(context, "Detected call hangup event", Toast.LENGTH_LONG).show();
}
}

public void sendCallEvent(String incomingNumber){
WritableMap params = Arguments.createMap();
params.putString("Number", incomingNumber);
sendEvent("CallRecevied", params);
}


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

最佳答案

由于 Fury 的解决方案在广播接收器中对我不起作用(getApplication() 出现“找不到符号”),我尝试应用类似的逻辑,我知道这是有效的。所以:

import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.ReactApplication;
...
// context - is the context you get from broadcastreceivers onReceive
ReactApplication rnApp = (ReactApplication) context.getApplicationContext();

rnApp.getReactNativeHost().getReactInstanceManager()
.getCurrentReactContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("test", Arguments.createMap());

这对我有用。

关于java - 如何在 React Native 中从 Java 代码访问 ReactContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36716199/

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