JS realizes the domain to send the message to the native interface and jumps the function _javascript skill

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.