gpt4 book ai didi

android - onNewIntent() 未在 ReactContextBaseJavaModule 上调用(react-native)

转载 作者:行者123 更新时间:2023-11-29 19:09:54 35 4
gpt4 key购买 nike

我正在构建一个 React native 模块,我从我的模块发送一个 PendingIntent,就像这样。

Intent postAuthorizationIntent = new Intent("com.example.HANDLE_AUTHORIZATION_RESPONSE");
PendingIntent pendingIntent = PendingIntent.getActivity(reactContext.getApplicationContext(), request.hashCode(), postAuthorizationIntent, 0);

如果我修改 MainActivity,那么我可以在 onNewIntent() 方法中接收数据。类看起来像这样......

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
}
}
}
}

然后,当我尝试将此逻辑移动到我的模块时,没有任何反应,它不起作用。

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) 修复了它:

@Override
public void onNewIntent(Intent intent) {
setIntent(intent);
super.onNewIntent(intent);
}

有了这个,onNewIntent 在您的模块中被调用 - 假设您的模块实现了 ActivityEventListener 并且您已经在构造函数中将其注册为监听器:

reactContext.addActivityEventListener(this);

关于android - onNewIntent() 未在 ReactContextBaseJavaModule 上调用(react-native),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45744013/

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