RNVodeAdModule.java 2.0 KB

package com.reactlibrary;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.Callback;

public class RNVodeAdModule extends ReactContextBaseJavaModule {

  private final ReactApplicationContext reactContext;

  public RNVodeAdModule(ReactApplicationContext reactContext) {
    super(reactContext);
    this.reactContext = reactContext;
  }

  @Override
  public String getName() {
    return "IntentMoudle";
  }

  @ReactMethod
  public void gotoNative( String params){
    try{

      Activity currentActivity = getCurrentActivity();
      if(null!=currentActivity){
        Class toActivity = Class.forName("com.reactlibrary.MyActivity");
        Intent intent = new Intent(currentActivity,toActivity);
        intent.putExtra("params", params);
        currentActivity.startActivity(intent);
      }
    }catch(Exception e){
      throw new JSApplicationIllegalArgumentException(
              "不能打开Activity : "+e.getMessage());
    }
  }

  @ReactMethod
  public void dataToJS(Callback successBack, Callback errorBack){
    try{
      Activity currentActivity = getCurrentActivity();
      String result = currentActivity.getIntent().getStringExtra("data");
      if (TextUtils.isEmpty(result)){
        result = "没有数据";
      }
      successBack.invoke(result);
    }catch (Exception e){
      errorBack.invoke(e.getMessage());
    }
  }


  @ReactMethod
  public void backJS()
  {
    try{
      Activity currentActivity = getCurrentActivity();
      if(null!=currentActivity){
        currentActivity.finish();
      }

    }catch (Exception e){
      throw new JSApplicationIllegalArgumentException(
              "不能返回js : "+e.getMessage());
    }
  }
}