The example of this article for everyone to share the JS interface for the original message and jump to the specific code, for your reference, the specific content as follows
Step One
In idea, open the./android/app under the RN project, this process takes a little time, and may need to download gradle dependencies or something.
Step Two
It's not bad to be a native app, we create a new testactivity, for simplicity's sake, just the following:
public class Testactivity extends Appcompatactivity {
private Button mbtgoback;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_test);
Mbtgoback = (Button) Findviewbyid (r.id.bt_go_back);
Mbtgoback.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (view view) {
Finish ();}}
);
}
Step three
Write a class Exampleinterface extends reactcontextbasejavamodule to receive messages in this class.
Specific code:
public class Exampleinterface extends Reactcontextbasejavamodule {
private reactapplicationcontext Mrapplicationcontext;
Public Exampleinterface (Reactapplicationcontext reactcontext) {
super (reactcontext);
Mrapplicationcontext = Reactcontext;
}
RN uses this name to invoke other functions of the native module
@Override public
String GetName () {return
"Exampleinterface";
}
@reactmethod must be written to register as a function that can be called by react
@ReactMethod public
void Handlermessage (String amessage) {
LOG.D ("LT", "====receive message from rn===" +amessage);
This section realizes a simple jump
Intent Intent = new Intent (mrapplicationcontext,testactivity.class);
Intent.addflags (intent.flag_activity_new_task);
Mrapplicationcontext.startactivity (intent);
}
Step Four
Implements a package manager, and registers the class exampleinterface that receives the message.
The code is as follows:
public class Anexamplereactpackage implements Reactpackage {
@Override public
list<nativemodule> Createnativemodules (Reactapplicationcontext reactapplicationcontext) {
list<nativemodule> modules = new Arraylist<> ();
Modules.add (New Exampleinterface (Reactapplicationcontext));
return modules;
}
@Override public
list<class<. Extends javascriptmodule>> createjsmodules () {return
Collections.emptylist ();
}
@Override public
list<viewmanager> createviewmanagers (Reactapplicationcontext reactapplicationcontext) {return
collections.emptylist ();
}
}
Step Five
Add package management class Anexamplereactpackage to mainapplication;
@Override
protected list<reactpackage> getpackages () {return
arrays.<reactpackage>aslist (
new Mainreactpackage (),
new Anexamplereactpackage ()
);
Step Six
in the JS interface, send messages;
Buttonpress:function () {
NativeModules.ExampleInterface.HandlerMessage (' test ');
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.