Android Open Source - JSBridge Main Activity






From Project

Back to project page JSBridge.

License

The source code is released under:

MIT License

If you think the Android project JSBridge listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.siva4u.main;
/*ww w.j  av a  2  s.co m*/
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

import com.siva4u.jsbridge.R;
import com.siva4u.jsbridge.JSBridge;
import com.siva4u.jsbridge.JSBridgeCallback;
import com.siva4u.jsbridge.JSBridgeHandler;
import com.siva4u.jsbridge.example.TestAPIOne;

public class MainActivity extends Activity implements JSBridgeHandler {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    final WebView webview = (WebView) findViewById(R.id.JSBridgeWebView);
    final JSBridge thisBridge = new JSBridge(this, getApplicationContext(), webview);
    
    thisBridge.registerJavaScriptAPI(new TestAPIOne(getApplicationContext(), webview));
    
    thisBridge.registerEvent("testObjcCallback", new JSBridgeHandler(){
      @Override
      public void hanlder(JSONObject data, JSBridgeCallback responseCallback) {
        System.out.println("JSBridgeWebViewApp: MainActivity: HandlerTestObjcCallback: "+data);
        try {
          responseCallback.callBack(new JSONObject("{'data':'Response from testObjcCallback'}"));
        } catch (JSONException e) {
          e.printStackTrace();
        }      
      }
    });
    
    JSONObject data = null;
    try {
      data = new JSONObject("{'data':'A string sent from ObjC before Webview has loaded.'}");
    } catch (JSONException e) {}
    
    thisBridge.send(null, data, new JSBridgeCallback(){
      @Override
      public void callBack(JSONObject data) {
        System.out.println("JSBridgeWebViewApp: MainActivity: CallbackSendMessage1: "+data);        
      }
    });
    
    try {
      data = new JSONObject("{'foo':'before ready'}");
    } catch (JSONException e) {}
    thisBridge.send("testJavascriptHandler",data,null);
    
    thisBridge.loadHTML("file:///android_asset/index.html");
    
    try {
      data = new JSONObject("{'data':'A string sent from ObjC after Webview has loaded.'}");
    } catch (JSONException e) {}
    thisBridge.send(null, data, null);
    
    final Button btnSendMsg = (Button) findViewById(R.id.btnSendMessage);
    btnSendMsg.setOnClickListener(new View.OnClickListener() {      
      @Override
      public void onClick(View v) {
        JSONObject data = null;
        try {
          data = new JSONObject("{'data':'A string sent from ObjC to JS'}");
        } catch (JSONException e) {}
        thisBridge.send(null,data,new JSBridgeCallback() {
          @Override
          public void callBack(JSONObject data) {
            System.out.println("JSBridgeWebViewApp: MainActivity: sendMessage got response: "+data);        
          }
        });
      }
    });
    final Button btnSendEvent = (Button) findViewById(R.id.btnSendEvent);
    btnSendEvent.setOnClickListener(new View.OnClickListener() {      
      @Override
      public void onClick(View v) {
        JSONObject data = null;
        try {
          data = new JSONObject("{'greetingFromObjC':'Hi there, JS!'}");
        } catch (JSONException e) {}
        thisBridge.send("testJavascriptHandler",data,new JSBridgeCallback() {
          @Override
          public void callBack(JSONObject data) {
            System.out.println("JSBridgeWebViewApp: MainActivity: testJavascriptHandler responded: "+data);        
          }
        });
      }
    });    
    }
    
  @Override
  public void hanlder(JSONObject data, JSBridgeCallback responseCallback) {
    System.out.println("JSBridgeWebViewApp: MainActivity: hanlder: START:\n"+data);
    try {
      responseCallback.callBack(new JSONObject("{'data':'Response for message from ObjC'}"));
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }
}




Java Source Code List

com.siva4u.jsbridge.JSBridgeAPIBase.java
com.siva4u.jsbridge.JSBridgeCallback.java
com.siva4u.jsbridge.JSBridgeHandler.java
com.siva4u.jsbridge.JSBridgeInteface.java
com.siva4u.jsbridge.JSBridge.java
com.siva4u.jsbridge.example.TestAPIOne.java
com.siva4u.main.MainActivity.java