【发布时间】:2017-08-17 19:58:55
【问题描述】:
我正在构建一个 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
}
}
}
}
【问题讨论】:
-
“它不起作用”是什么意思?请提供任何错误消息。
-
对不起,我的意思是......什么也没发生,onNewIntent 方法从未被调用,我在控制台上看不到任何错误,应用程序继续执行。正如我所说,相同的代码在 MainActivity 中运行良好。
标签: android react-native