Android - 如何从 JavaScript 运行意图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7233684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:24:53  来源:igfitidea点击:

Android - How to run intent from JavaScript

javajavascriptandroidemailwebview

提问by user899641

I am new to Android and Java. I have constructed an app using HTML/Javascript that is working great.

我是 Android 和 Java 的新手。我使用 HTML/Javascript 构建了一个运行良好的应用程序。

I now need to create an activity that launches the email client, fills in subject and body, and (the tough part) adds a file attachment. I have not been able to do this from within JavaScript, mailto:will not attach the file.

我现在需要创建一个活动来启动电子邮件客户端,填写主题和正文,并且(困难的部分)添加一个文件附件。我无法在 JavaScript 中执行此操作,mailto:因此不会附加文件。

So I need to accomplish this through Java and execute it from JavaScript. I think this can be done by using addJavaScriptInterfacebut I cannot find any detailed documentation or examples to go off of.

所以我需要通过 Java 来完成它并从 JavaScript 中执行它。我认为这可以通过使用来完成,addJavaScriptInterface但我找不到任何详细的文档或示例。

How could I do this?

我怎么能这样做?

Here is what I have so far after reading the documentation:

这是我阅读文档后到目前为止的内容:

2nd update to code:

第二次更新代码:

MainActivity.java

主活动.java

public class MainActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setIntegerProperty( "splashscreen", R.drawable.splash );
    super.loadUrl("file:///android_asset/www/index.html", 1000);
    WebView mWebView;
    mWebView = (WebView)findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.addJavascriptInterface(new JavaScriptInterface(), "Android"); 
}
}

JavaScriptInterface.java

JavaScript接口.java

public class JavaScriptInterface {

public void doEmail(){
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType("text/html");
    sendIntent.putExtra(android.content.Intent.EXTRA_TEXT,"test text");
    sendIntent.putExtra(Intent.EXTRA_SUBJECT,"test subject");
    sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://test co.html"));
    startActivity(Intent.createChooser(sendIntent, "Send email..."));
    } 
}

Then I would reference the intent through JavaScript by using Android.doEmail().

然后我将使用 Android.doEmail() 通过 JavaScript 引用意图。

With the above code I am getting 2 errors in Eclipse 1. The method startActivity(Intent) is undefined for the type - JavaScriptInterface 2. webview cannot be resolved or is not a field - MainActivity

使用上面的代码,我在 Eclipse 1 中遇到 2 个错误。方法 startActivity(Intent) 未定义类型 - JavaScriptInterface 2. webview 无法解析或不是字段 - MainActivity

What am I doing wrong?

我究竟做错了什么?

采纳答案by adamcodes

public class MainActivity extends DroidGap {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setIntegerProperty( "splashscreen", R.drawable.splash );

        JavaScriptInterface jsi = new JavaScriptInterface(this, appView);
        appView.addJavascriptInterface(jsi, "Android");

        super.loadUrl("file:///android_asset/www/index.html", 1000);
    }
}

and

public class JavaScriptInterface {
    private WebView mAppView;
    private DroidGap mGap

    public JavaScriptInterface (DroidGap gap, WebView view)
    {
        mAppView = view;
        mGap = gap;
    }

    public void doEmail(){
        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.setType("text/html");
        sendIntent.putExtra(android.content.Intent.EXTRA_TEXT,"test text");
        sendIntent.putExtra(Intent.EXTRA_SUBJECT,"test subject");
        sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://test co.html"));
        startActivity(Intent.createChooser(sendIntent, "Send email..."));
    } 
}

回答by nicholas.hauschild

This documentationtells you exactly how to do it.

本文档准确地告诉您如何操作。

It looks like there are three main steps:

看起来主要分为三个步骤:

  1. Create your 'interface' class in Android
  2. Add an instance of this 'interface' to the WebViewyou are using.
  3. Call the interface from your JavaScript.
  1. 在 Android 中创建您的“界面”类
  2. 将此“接口”的实例添加到WebView您正在使用的对象中。
  3. 从您的 JavaScript 调用接口。

回答by Steve

Using addJavaScriptInterfacewill extend the DOM inside the embedded browser, and allow JS to access a Java object, which is exactly what you want.

UsingaddJavaScriptInterface将扩展嵌入式浏览器内部的 DOM,并允许 JS 访问 Java 对象,这正是您想要的。

There are too many steps to outline here, that have already been documented. This linkhas a good overview.

此处要概述的步骤太多,已记录在案。这个链接有一个很好的概述。

回答by bryanallott

I used WebIntents from Boris Smus (http://smus.com/android-phonegap-plugins) and it works like a charm. You can also peruse his code a little to understand better the approach he took with plugins.

我使用了来自 Boris Smus (http://smus.com/android-phonegap-plugins) 的 WebIntents,它的作用就像一个魅力。您还可以仔细阅读他的代码以更好地理解他使用插件的方法。

NOTE: you do need to update the code provided as is a little (see comments) and the plugin architecture has changed a little.

注意:您确实需要稍微更新提供的代码(请参阅评论),并且插件架构也发生了一些变化。