问题描述
我正在构建一个react native模块,从我的模块中我发送这样的PendingIntent.
I'm building a react native module, from my module I send a PendingIntent like this.
Intent postAuthorizationIntent = new Intent("com.example.HANDLE_AUTHORIZATION_RESPONSE");
PendingIntent pendingIntent = PendingIntent.getActivity(reactContext.getApplicationContext(), request.hashCode(), postAuthorizationIntent, 0);
如果修改MainActivity,则可以在onNewIntent()方法内接收数据.班级看起来像这样……
If I modify the MainActivity then I can receive data inside onNewIntent() method. The class looks like this...
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "example";
}
@Override
public void onNewIntent(Intent intent) {
checkIntent(intent);
}
@Override
protected void onStart() {
super.onStart();
checkIntent(getIntent());
}
private void checkIntent(@Nullable Intent intent) {
if (intent != null) {
String action = intent.getAction();
switch (action) {
case "com.example.HANDLE_AUTHORIZATION_RESPONSE":
...
break;
default:
// do nothing
}
}
}
}
然后,当我尝试将此逻辑移至模块时,什么也没有发生,这是行不通的.
Then when I try to move this logic to my Module, nothing happens, it does not work.
public class ExampleModule extends ReactContextBaseJavaModule implements ActivityEventListener{
private ReactApplicationContext reactContext;
private String action = "";
public ExampleModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
this.action = "com.example.HANDLE_AUTHORIZATION_RESPONSE";
reactContext.addActivityEventListener(this);
}
@Override
public String getName() {
return "ExampleModule";
}
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
}
@Override
public void onNewIntent(Intent intent) {
checkIntent(intent);
}
private void checkIntent(@Nullable Intent intent) {
if (intent != null) {
String action = intent.getAction();
switch (action) {
case "com.example.HANDLE_AUTHORIZATION_RESPONSE":
...
break;
default:
// do nothing
}
}
}
}
推荐答案
我遇到了同样的问题,并通过从 MainActivity
调用 super.onNewIntent(intent)
来解决此问题:
I had this same issue and fixed it by calling super.onNewIntent(intent)
from MainActivity
:
@Override
public void onNewIntent(Intent intent) {
setIntent(intent);
super.onNewIntent(intent);
}
在此位置上,在模块中调用 onNewIntent
-假设您的模块实现了 ActivityEventListener
,并且您已将其注册为构造函数中的侦听器:
With this in place, onNewIntent
is called in your module - assuming your module implements ActivityEventListener
and you've registered it as a listener in the constructor:
reactContext.addActivityEventListener(this);
这篇关于在ReactContextBaseJavaModule上未调用onNewIntent()(react-native)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!