安卓原生
public class CommunicationModule extends ReactContextBaseJavaModule {
public CommunicationModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "CommunicationModule";
}
//RN ---> Android 通过参数进行传递
@ReactMethod
public void RNtoAndroid(String str1,String str2){
Toast.makeText(getReactApplicationContext(),str1+":"+str2,Toast.LENGTH_SHORT).show();
}
//Android ---> RN 通过回调函数传递
@ReactMethod
public void AndroidToRNCallback(Callback callback){
callback.invoke("Android");
}
//Android ---> RN 通过异步编程传递
@ReactMethod
public void AndroidToRnPromise(String str, Promise promise){
//Rn给Android传递数据
Toast.makeText(getReactApplicationContext(),str,Toast.LENGTH_SHORT).show();
//android给rn传递数据
promise.resolve("Android");
}
、、、、跳转android
@ReactMethod
public void RNGOTO(String name,String age){
Intent intent = new Intent(getReactApplicationContext(),Main2Activity.class);
intent.putExtra("name",name);
intent.putExtra("age",age);
getReactApplicationContext().startActivity(intent);
}
}
、、。。。安卓条RN
public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initView();
}
private void initView() {
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
Intent intent = new Intent(HomeActivity.this,MainActivity.class);
HomeActivity.this.startActivity(intent);
break;
}
}
}