2017-08-17 39 views
1

我正在構建一個反應原生模塊,從我的模塊發送PendingIntent這樣。onNewIntent()不在ReactContextBaseJavaModule(react-native)上調用

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

你是什麼意思,「它不工作」?請提供任何錯誤消息。 –

+0

對不起,我的意思是......什麼都沒有發生,onNewIntent方法從不被調用,並且我沒有看到控制檯上的任何錯誤,並且應用程序繼續執行。正如我所說的,相同的代碼運行良好,但在MainActivity內。 –

回答

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()); 
    } 

}