Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

javascript - How to run a Java Script function and get return in Android?

I'm working on a project in Xamarin C# and I'm having trouble running some Java Script. Is there currently a way to call a specific JS function and receive its return value?

I know that JS can be run in a WebView; however, how can I get the output?

At the moment, the JS is coming from a link on my site. Any help would be very much appreciated!


EDIT

Here's the code I've tried:

MyView.axml

 <android.webkit.WebView
            android:id="@+id/web_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            local:layout_constraintTop_toBottomOf="@+id/pause"
            local:layout_constraintBottom_toBottomOf="parent"/>

MyView.cs

called in protected override void OnCreate

webView.Settings.JavaScriptEnabled = true;
webView.SetWebViewClient(new MyWebViewClient());
webView.LoadUrl("http://www.example.com/scheduleV5.js");

MyWebViewClient (class)

public class MyWebViewClient : WebViewClient
{
    public override void OnPageFinished(WebView view, String url)
    {
        view.EvaluateJavascript("javascript: getSchedule();", new EvaluateBack());
    }
}

EvaluateBack (class)

public class EvaluateBack : Java.Lang.Object, IValueCallback
{

    public void OnReceiveValue(Java.Lang.Object value)
    {
        Toast.MakeText(Android.App.Application.Context, value.ToString(), ToastLength.Short).Show();// you will get the value "100"
        var test = value.ToString();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you could do like this

Js calls methods in C#

Configure the OnCreate method in the Activity :

var webview = FindViewById<WebView>(Resource.Id.webView1);
WebSettings settings = webview.Settings;
settings.JavaScriptEnabled = true;
// load the javascript interface method to call the foreground method
webView.AddJavascriptInterface(new MyJSInterface(this), "CSharp");
webview.SetWebViewClient(new WebViewClient());

Create a C# class :

class MyJSInterface : Java.Lang.Object
  {
    Context context;

  public MyJSInterface (Context context)
    {
      this.context = context;
    }

  [JavascriptInterface]
  [Export]
  public void ShowToast (string msg)
    {
      Toast.MakeText(context, msg, ToastLength.Short).Show();
    }
  }

And then it's called in JS :

<button type="button" onClick="CSharp.ShowToast ('Call C#')">Call C#</button>

You can refer to this document:https://github.com/xamarin/recipes/tree/master/Recipes/android/controls/webview/call_csharp_from_javascript

C# calls a method in JS and gets the callback value

webView.EvaluateJavascript("javascript: callJS();", new EvaluateBack());

class EvaluateBack : Java.Lang.Object,IValueCallback
    {

        public void OnReceiveValue(Object value)
        {

            Toast.MakeText(Android.App.Application.Context, value.ToString(), ToastLength.Short).Show();// you will get the value "100"
        }
    }

in JS :

<script>
      function callJS(){
        return 100;
      }
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...