【问题标题】:Android- mailto link won't workAndroid-mailto 链接不起作用
【发布时间】:2013-04-03 11:41:33
【问题描述】:

我在 stackoverflow 上阅读了很多关于这个主题的问题,我的解决方案也来自这里,但它不起作用。也没有错误信息。

我在 Android 中有一个 webview 和一个主页,有一些 mailto 链接,当我尝试在 mailclient 中从我的 webview 打开 mailto 链接时,webview 总是像普通网页一样打开邮件地址。

有人可以看看我的代码并告诉我出了什么问题吗?

感谢您的帮助。

代码:

清单文件:

1<?xml version="1.0" encoding="utf-8"?>
2<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.example.TEST"
4 android:versionCode="1"
5 android:versionName="1.5" android:installLocation="auto">
6
7 <uses-permission android:name="android.permission.INTERNET" />
8 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
9 <uses-permission android:name="android.permission.ACCESS_GPS" />
10  <uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
11  <uses-permission android:name="android.permission.ACCESS_LOCATION" />
12  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
13
14 <uses-sdk
15 android:minSdkVersion="8"
16 android:targetSdkVersion="17" />
17
18 <application
19 android:allowBackup="true"
20 android:icon="@drawable/ic_launcher"
21 android:label="@string/app_name"
22 android:theme="@style/AppTheme" >
23 <activity
24 android:name="com.example.lauffinderpro.MainActivity"
25 android:label="@string/app_name" >
26 <intent-filter>
27 <action android:name="android.intent.action.MAIN" />
28
29 <category android:name="android.intent.category.LAUNCHER" />
30 </intent-filter>
31 </activity>
32 </application>
33
34</manifest>

ma​​in.xml

1<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:id="@+id/FrameLayout1"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:paddingBottom="@dimen/vertical"
7 android:paddingLeft="@dimen/horizontal"
8 android:paddingRight="@dimen/horizontal"
9 android:paddingTop="@dimen/vertical"
10 tools:context=".MainActivity" >
11
12 <LinearLayout
13 android:layout_width="match_parent"
14 android:layout_height="match_parent"
15 android:orientation="vertical" >
16
17 <WebView
18 android:id="@+id/webView1"
19 android:layout_width="match_parent"
20 android:layout_height="0dp"
21 android:layout_weight="0.05" />
22
23 <FrameLayout
24 android:layout_width="match_parent"
25 android:layout_height="wrap_content"
26 android:layout_weight="0.00" >
27
28 <LinearLayout
29 android:layout_width="match_parent"
30 android:layout_height="wrap_content"
31 android:baselineAligned="true" >
32
33
34 <Button
35 android:id="@+id/button1"
36 android:layout_width="0dp"
37 android:layout_weight="2"
38 android:layout_height="match_parent"
39 android:background="@drawable/upgradetest"
40 android:onClick="button1click" />
41
42 <Button
43 android:id="@+id/button2"
44 style="?android:attr/buttonStyleSmall"
45 android:layout_width="0dp"
46 android:layout_weight="2"
47 android:layout_height="match_parent"
48 android:background="@drawable/globaltest"
49 android:onClick="button2click" />
50
51 <Button
52 android:id="@+id/button3"
53 style="?android:attr/buttonStyleSmall"
54 android:layout_width="0dp"
55 android:layout_weight="2"
56 android:layout_height="match_parent"
57 android:background="@drawable/datetest"
58 android:onClick="button3click" />
59
60 <Button
61 android:id="@+id/button5"
62 style="?android:attr/buttonStyleSmall"
63 android:layout_width="0dp"
64 android:layout_weight="2"
65 android:layout_height="match_parent"
66 android:layout_gravity="right"
67 android:background="@drawable/mehrtest"
68 android:onClick="button5click" />
69 </LinearLayout>
70
71 </FrameLayout>
72
73 </LinearLayout>
74
75</FrameLayout>

ma​​in.java

1package com.example.lauffinderpro;
2
3import java.net.URISyntaxException;
4
5import android.net.MailTo;
6import android.net.Uri;
7import android.os.Bundle;
8import android.annotation.SuppressLint;
9import android.app.Activity;
10import android.app.AlertDialog;
11import android.content.Context;
12import android.content.DialogInterface;
13import android.content.Intent;
14import android.util.Log;
15import android.view.KeyEvent;
16import android.view.Menu;
17import android.view.View;
18import android.view.View.OnClickListener;
19import android.webkit.GeolocationPermissions;
20import android.webkit.WebChromeClient;
21import android.webkit.WebView;
22import android.webkit.WebViewClient;
23import android.widget.Button;
24import android.widget.ImageButton;
25
26public class MainActivity extends Activity {
27  
28  
29  protected void onCreate(Bundle savedInstanceState) {
30  super.onCreate(savedInstanceState);
31  setContentView(R.layout.activity_main);
32  
33  
34  //startet Upgrade als Standard
35  
36  @SuppressLint("SetJavaScriptEnabled")
37  
38  
39  WebView myWebView = (WebView) findViewById (R.id.webView1);
40  myWebView.getSettings().setJavaScriptEnabled(true);
41  myWebView.loadUrl("WEBSITE");
42  myWebView.setWebViewClient(new WebViewClient());
43  
44  
45  }
46  
47  
48  
49  /*public boolean onCreateOptionsMenu(Menu menu) {
50  // Inflate the menu; this adds items to the action bar if it is present.
51  getMenuInflater().inflate(R.menu.main, menu);
52  return true;
53  }
54  */
55
56  
57  public boolean shouldOverrideUrlLoading(WebView view, String url) {
58  if (url.startsWith("mailto:")) {
59  Intent intent = new Intent(Intent.ACTION_VIEW,
60  Uri.parse(url));
61  startActivity(intent);
62  }else if(url.startsWith("http:") || url.startsWith("https:")) {
63  view.loadUrl(url);
64  }
65  return true;
66  }
67  
68  
69  public void button1click (View view) {
70  
71  Code
72  
73  }
74  
75  
76  public void button2click (View view) {
77  
78  Code
79  
80  }
81
82
83  
84  public void button3click (View view) {
85  
86  Code
87  
88  }
89  
90  
91  
92  public void button5click (View view) {
93  
94  Code
95  
96  }
97
98  
99  
100}

编辑:

如果我尝试这样做,我会收到错误消息“方法 replaseFirst(String) 未定义 String 类型”。为此,我将url.replaseFirst 更改为url.replaceFirst,但他需要第二个参数,所以我必须写url.replaceFirst("mailto:", X );。对于 X,我可以输入 mailurlnull。我尝试了每个人,每个人都收到我必须添加退货声明的消息,所以我用return false 做到了。 现在我有了这个代码:

public class MyWebViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(url.startsWith("mailto:")) {
                      String mail = url.replaceFirst ("mailto:", mail);
                      Intent intent = new Intent(Intent.ACTION_SEND);
                      intent.setType("message/rfc822");
                      intent.putExtra(Intent.EXTRA_EMAIL, mail );


                      startActivity(Intent.createChooser(intent, "Send Email"));
             }
                return false;
        }
    }

这不起作用。所以我尝试intent.setType("message/rfc822"); 而不是intent.setType("text/plain"); 但这也行不通。

我做错了什么?

EDIT2:

新代码:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.startsWith("mailto:")) {
                String mail = url.replaceFirst("mailto:", "");
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_EMAIL, mail );

                startActivity(Intent.createChooser(intent, "Send Email"));
             }  
            else if(url.startsWith("http:") || url.startsWith("https:")) {
                    view.loadUrl(url);
                }
                return true;
        }

【问题讨论】:

    标签: android hyperlink mailto


    【解决方案1】:

    要强制打开邮件客户端,您可以尝试:

    if(url.startsWith("mailto:")) {
      String mail = url.replaceFirst("mailto:", "");
      Intent intent = new Intent(Intent.ACTION_SEND);
      intent.setType("text/plain");
      intent.putExtra(Intent.EXTRA_EMAIL, mail );
      // intent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); // if you want extra
      // intent.putExtra(Intent.EXTRA_TEXT, "I'm email body."); // if you want extra
    
      startActivity(Intent.createChooser(intent, "Send Email"));
    } else if ... {
       // your code as is
    }
    

    但其他“发送”应用也打开,取决于 Intent.ACTION_SEND 的支持

    建议使用intent.setType("message/rfc822"); 而不是intent.setType("text/plain"); here,但我从未测试过。


    编辑:

    更改 main.java 的行:

    // ...
    WebView myWebView = (WebView) findViewById (R.id.webView1);
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.setWebViewClient(new MyWebViewClient()); // you name your class MyWebViewClient not WebViewClient
    myWebView.loadUrl("WEBSITE"); // also change line order
    // ...
    

    【讨论】:

    • 请查看我的编辑,我无法回答自己的问题,也不知道如何在评论中显示代码。
    • @TomHans 对不起我的错误,我修复了代码。方法是url.replaceFirst("mailto:", ""); 第一个字符串是要替换的字符串,第二个字符串是要替换的字符串!您设置一个空字符串,它就像一个没有"mailto:" 的子字符串。
    • 感谢您的指正。现在我没有错误消息了,但它仍然无法正常工作。我的代码在我的 EDIT2 中看起来像这样,我的代码有问题吗?
    • 但这不起作用。如果我单击邮件链接,webview 会尝试像普通链接一样打开它。只有代码没有任何错误。
    • 如果您的 html 代码正确,是否在正确的位置调用了正确的方法? &lt;a href="mailto:yourmail@example.com" target="_blank"&gt;yourmail@example.com&lt;/a&gt; target="_blank" 可能不需要...也交换 main.java 中的第 41、42 行,更合理的是先初始化 webview,然后在其中加载代码。
    【解决方案2】:

    我的例子:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_EMAIL,  new String[]{"THE_EMAIL_ADDRESS_WITHOUT_mailto:"});
    startActivity(Intent.createChooser(intent, "Send Email"));
    

    注意字符串数组。如果你只传递一个字符串,它不会被考虑。

    【讨论】:

      【解决方案3】:

      虽然这是一个老问题,但我发现上面的答案都没有 100% 对我有用。我只想打开电子邮件应用程序,而不是短信等。

      我改用了这段代码,(没有意图.SetType)

      String[] email_address = new String[] {getString(R.string.email_address)};
      Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", getString(R.string.email_address), null));
      intent.putExtra(Intent.EXTRA_EMAIL, email_address);
      intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject_question));
      startActivity(Intent.createChooser(intent, "Send Email"));
      

      额外的一行:intent.putExtra(Intent.EXTRA_EMAIL, email_address);所以它适用于所有版本的android。

      【讨论】:

        猜你喜欢
        • 2018-04-14
        • 2012-05-26
        • 2011-12-14
        • 2017-07-09
        • 1970-01-01
        • 2012-09-17
        • 2017-12-25
        • 2020-02-04
        • 2012-07-24
        相关资源
        最近更新 更多