我已閱讀了很多關於此主題的問題在這裏在stackoverflow,我的解決方案來自這裏,但它不會工作。也沒有錯誤信息。Android的mailto鏈接將無法正常工作
我在Android和一個主頁中有一個webview,有一些mailto鏈接,當我嘗試從我的webview在mailclient中打開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.replaceFirst
,但他需要一個secound的論點,所以我必須寫url.replaceFirst("mailto:", X);
。對於X我可以鍵入mail
,url
或null
。我嘗試了每個人,每次我收到消息,我必須添加一個返回語句,所以我做了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;
}
請看我的編輯,我無法回答我自己的問題,我不知道如何在評論中使代碼視圖。 – 2013-04-11 13:32:38
@TomHans爲我的錯誤sry,我修復了代碼。該方法是'url.replaceFirst(「mailto:」,「」);'第一個字符串是你想要替換的,第二個是被替換的東西!你設置一個空字符串就像一個沒有'「mailto:」'''的子字符串。 – madlymad 2013-04-12 07:14:46
謝謝你的糾正。 現在我再也沒有錯誤信息,但它仍然不起作用。 我的代碼在我的EDIT2中看起來像這樣,反正有我的代碼有問題嗎? – 2013-04-12 09:11:40