Android- mailto链接无效
作者:互联网
我在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>
main.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>
main.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)未定义类型字符串”.因此我将url.replaseFirst更改为url.replaceFirst,但他需要一个有争议的参数,所以我必须写url.replaceFirst(“mailto:”,X);.对于X,我可以键入mail,url或null.我尝试了每个人,每个人都得到消息,我必须添加一个return语句,所以我用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;
}
解决方法:
要强制打开邮件客户端,您可以尝试:
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(“text / plain”); here建议使用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
// ...
标签:android,hyperlink,mailto 来源: https://codeday.me/bug/20190529/1178751.html