如何在 Android 中运行 Java Script 函数并获得回报? [英] How to run a Java Script function and get return in Android?

查看:22
本文介绍了如何在 Android 中运行 Java Script 函数并获得回报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Xamarin C# 中处理一个项目,但在运行某些 Java 脚本时遇到问题.目前有没有办法调用特定的 JS 函数并接收其返回值?

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?

我知道JS可以在WebView中运行;但是,我怎样才能得到输出?

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

目前,JS 来自我网站上的链接.任何帮助将不胜感激!

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

这是我试过的代码:

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

protected override void OnCreate

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

MyWebViewClient(类)

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

EvaluateBack(类)

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

推荐答案

你可以这样做

Js 调用 C# 中的方法

在Activity中配置OnCreate方法:

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

创建一个C#类:

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

然后在JS中调用:

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

可以参考这个文档:https://github.com/xamarin/recipes/tree/master/Recipes/android/controls/webview/call_csharp_from_javascript

C#调用JS中的方法并获取回调值

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"
        }
    }

在JS中:

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

这篇关于如何在 Android 中运行 Java Script 函数并获得回报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆