Pass and return values ​​from javascript and android and use as a plugin for your phone

I want to create a plugin for a phone that transfers and returns a value between javascript and android.

Can anyone suggest any ideas on how to do this?

+5
source share
2 answers

Actually, this is not very difficult. Here I will show you how to call native code from javascript inside the page and vice versa:

Calling native code from a web view :
When creating a web view, add a javascript interface (basically a java class whose methods will be displayed through javascript in the web view.

JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "JSInterface");

javascript ( , )

public class JavaScriptInterface {
    private Activity activity;

    public JavaScriptInterface(Activity activiy) {
        this.activity = activiy;
    }

    public void startVideo(String videoAddress){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(videoAddress), "video/3gpp"); // The Mime type can actually be determined from the file
        activity.startActivity(intent);
    }
}

, HTML , :

<script>
    function playVideo(video){
        window.JSInterface.startVideo(video);
    }
</script>

, ?

javascript :
, HTML, WebView, javascript:

<script>
    function function(){
        //... do something
    }
</script>

WebView :

webView.loadUrl("javascript:function()");
+14

PhoneGap. ChildBrowser.

0

All Articles