Search in sources :

Example 1 with BridgeHandler

use of com.github.lzyzsd.jsbridge.BridgeHandler in project JsBridge by lzyzsd.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = (BridgeWebView) findViewById(R.id.webView);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);
    webView.setDefaultHandler(new DefaultHandler());
    webView.setWebChromeClient(new WebChromeClient() {

        @SuppressWarnings("unused")
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType, String capture) {
            this.openFileChooser(uploadMsg);
        }

        @SuppressWarnings("unused")
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType) {
            this.openFileChooser(uploadMsg);
        }

        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
            mUploadMessage = uploadMsg;
            pickFile();
        }
    });
    webView.loadUrl("file:///android_asset/demo.html");
    webView.registerHandler("submitFromWeb", new BridgeHandler() {

        @Override
        public void handler(String data, CallBackFunction function) {
            Log.i(TAG, "handler = submitFromWeb, data from web = " + data);
            function.onCallBack("submitFromWeb exe, response data 中文 from Java");
        }
    });
    User user = new User();
    Location location = new Location();
    location.address = "SDU";
    user.location = location;
    user.name = "大头鬼";
    webView.callHandler("functionInJs", new Gson().toJson(user), new CallBackFunction() {

        @Override
        public void onCallBack(String data) {
        }
    });
    webView.send("hello");
}
Also used : BridgeHandler(com.github.lzyzsd.jsbridge.BridgeHandler) WebChromeClient(android.webkit.WebChromeClient) Gson(com.google.gson.Gson) CallBackFunction(com.github.lzyzsd.jsbridge.CallBackFunction) Uri(android.net.Uri) DefaultHandler(com.github.lzyzsd.jsbridge.DefaultHandler)

Aggregations

Uri (android.net.Uri)1 WebChromeClient (android.webkit.WebChromeClient)1 BridgeHandler (com.github.lzyzsd.jsbridge.BridgeHandler)1 CallBackFunction (com.github.lzyzsd.jsbridge.CallBackFunction)1 DefaultHandler (com.github.lzyzsd.jsbridge.DefaultHandler)1 Gson (com.google.gson.Gson)1