5

I want to create a plugin for phone which pass and returns the value between javascript and android.

Can anybody suggest any ideas on how to do this?

user1051599
  • 371
  • 2
  • 7
  • 18

2 Answers2

14

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

Calling native code from within web view:
When creating the web view add javascript interface (basically java class whose methods will be exposed to be called via javascript in the web view.

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

The definition of the javascript interface class itself (this is exemplary class I took from another answer of mine and opens video in native intent)

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);
    }
}

Now if you want to call this code form the HTML of the page you provide the following method:

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

Easy isn't it?

Calling javascript code from native code:
This is also simple suppose in the code of the HTML loaded in WebView you have javascript function defined:

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

Then you call this function through the WebView in the native code like that:

webView.loadUrl("javascript:function()");
Komal12
  • 3,340
  • 4
  • 16
  • 25
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • Hey thanks Boris :D This is what I exactly looking for. I really like that you wrote in detail in a very nice simple way. Jgd, God Bless you :) – Prax Sep 24 '12 at 06:43
  • 1
    the call javascript code from native code works, but how do you alter it to return a value from the javascript to the java code – user280109 Dec 11 '12 at 15:24
  • @user280109: You need to add the parameter values in the call. Here is an example: http://stackoverflow.com/a/4330642/1108032 – Boris Strandjev Dec 11 '12 at 17:51
  • @BorisStrandjev How to get returned value in "Calling native code from within web view"? Imagine 'startVideo' method return "Nice Video!". How can I get it 'playVideo'? – Dr.jacky Feb 24 '17 at 12:46
  • @Mr.Hyde Something I can think of is to call the other way communication native to javascript within the called native code (before the finish of the method). This javascritp code will initialize javascript variables with the needed values. Maybe there is faster and better solution, but I do not know it by heart. – Boris Strandjev Feb 24 '17 at 14:08
  • @user280109 You able to find out `alter it to return a value from the javascript to the java code`? Is it possible? – Sam YC Jan 28 '20 at 09:07
  • @GMsoF note my method `startVideo` it already does that (the url of the video is exchanged from JS to java) – Boris Strandjev Jan 28 '20 at 09:29
0

Here's a tutorial for creating a PhoneGap Plugin. Also the instructions for the ChildBrowser plugin are especially good.

Paul Beusterien
  • 27,542
  • 6
  • 83
  • 139