Android-mailto链接赢得';行不通

Android-mailto链接赢得';行不通,android,hyperlink,mailto,Android,Hyperlink,Mailto,我在stackoverflow上读到了很多关于这个主题的问题,我的解决方案也来自这里,但它不起作用。此外,没有错误消息 我在Android中有一个webview和一个主页,有一些mailto链接,当我试图在mailclient中从我的webview打开mailto链接时,webview总是像普通网页一样打开邮件地址 有人能看看我的代码,告诉我出了什么问题吗 谢谢你的帮助 代码: 清单文件: 1<?xml version="1.0" encoding="utf-8"?> 2<ma

我在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>
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;
        }
编辑:

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>
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;
        }
如果我尝试这样做,我会得到错误消息“类型字符串的方法replaseFirst(String)未定义”。因此,我将
url.replaceFirst
更改为
url.replaceFirst
,但他需要一个可靠的参数,因此我必须编写
url.replaceFirst(“mailto:,X”)。对于X,我可以键入
mail
url
null
。我尝试过每个人,每次我都会得到一条消息,我必须添加一个return语句,所以我使用了
returnfalse
。 现在我有了这个代码:

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:

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>
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;
        }
新代码:

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

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

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.setType(“text/plain”)intent.setType(“message/rfc822”)但我从未测试过


编辑:

更改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
// ...
我的例子是:

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

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

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

我改用了这段代码(无意中使用了SetType)


额外的行:intent.putExtra(intent.extra\u电子邮件,电子邮件地址);它在所有版本的android上都能正常工作

请查看我的编辑,我无法回答自己的问题,我不知道如何在注释中创建代码视图。@TomHans sry由于我的错误,我修复了代码。方法是
url.replaceFirst(“mailto:,”)第一个字符串是要替换的字符串,第二个字符串是要替换的字符串!您设置了一个空字符串,它就像一个子字符串,其中没有
“mailto:
。感谢您的更正。现在我没有错误消息了,但它仍然不工作。我的代码在我的EDIT2中看起来像这样,我的代码有问题吗?但它不工作。如果我单击邮件链接,webview会尝试像普通链接一样打开它。只有代码没有任何错误。如果html代码正确,是否会在正确的位置调用正确的方法
可能不需要
目标=“\u blank”
。。。还可以先将main.java的第41行和第42行交换到init webview,然后在其中加载代码。