在ReactContextBaseJavaModule上没有调用onNewIntent()(react-native)

时间:2017-08-17 19:58:56

标签: android react-native

我正在构建一个react本机模块,从我的模块中我发送了一个像这样的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
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

好的,我解决了这个问题。

public class RNSsfAuthModule extends ReactContextBaseJavaModule implements ActivityEventListener, Application.ActivityLifecycleCallbacks{
    ....

    public ExampleModule(ReactApplicationContext reactContext, Application application) {
            super(reactContext);
            ...
            reactContext.addActivityEventListener(this);
            application.registerActivityLifecycleCallbacks(this);
          }

    ...


    @Override
    public void onActivityStarted(Activity activity) {
        checkIntent(activity.getIntent());
    }

}

答案 1 :(得分:0)

我遇到了同样的问题并通过从super.onNewIntent(intent)调用MainActivity来解决此问题:

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

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

reactContext.addActivityEventListener(this);