9

I have read many Questions about this topic here at stackoverflow, also my solution come from here but it won't work. Also there is no error message.

I have a webview in Android and a Homepage, there are some mailto links, when I try to open the mailto links from my webview in a mailclient, the webview always open the mail address like a normal webpage.

Could someone look at my code and tell my what's wrong?

Thank's for your help.

Code:

Manifest file:

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}

EDIT:

If I try this I get the error message "The method replaseFirst(String) is undefined for the type String". In cause of this I changed url.replaseFirst to url.replaceFirst but his needs a secound argument so I have to write url.replaceFirst("mailto:", X );. For X I can type mail,url or null. I tryed everyone, by each I get the message that I have to add a return statement, so I did it with return false. Now I have this code:

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

This does not work. So I try intent.setType("message/rfc822"); instead of intent.setType("text/plain"); but this also does not work.

What I have done wrong?

EDIT2:

new code:

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;
        }
Tom Hans
  • 123
  • 1
  • 1
  • 6

3 Answers3

7

To force the open of mail client you can try that:

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
}

but other "send" apps open too, depends of support of Intent.ACTION_SEND

Instead of intent.setType("text/plain"); here is suggested the use of intent.setType("message/rfc822"); but I have never tested.


Edit:

change lines of 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
// ...
Community
  • 1
  • 1
madlymad
  • 6,367
  • 6
  • 37
  • 68
  • Please look to my edit, I wasn't able to answer my own question and I don't know how to make the code view in a comment. – Tom Hans Apr 11 '13 at 13:32
  • @TomHans sry for my mistake, i fixed the code. The method is `url.replaceFirst("mailto:", "");` First string is what you want to replace, the second one is what to be replaced with! You set an empty string its just like a substring without the `"mailto:"` in it. – madlymad Apr 12 '13 at 07:14
  • Thank you for your correction. Now I have no error message anymore, but it is still not working. My code looks like this in my EDIT2, is there anyway a problem with my code? – Tom Hans Apr 12 '13 at 09:11
  • But it doesn't work. If I click the mail link the webview trys to open it like an normal link. Only the code doesn't have any error. – Tom Hans Apr 12 '13 at 11:27
  • If your html code right, does the right method get called in the right place? `yourmail@example.com` the `target="_blank"` may not needed... also swap lines 41, 42 at main.java its more logical to init webview first and then load code in it. – madlymad Apr 12 '13 at 13:27
  • The right methode should be called because my html code looks like this: `yourmail@example.com` so it the same. The lines 41, 42 are changed, your right that's more logical than mine and the `MyWebViewClient` was my mistake, sry. But I don't know what you mean with "is the code working for plain urls" that's confuses me. – Tom Hans Apr 12 '13 at 15:46
  • I have found my last problem, this one was in a line wich I didn't post, from my button3 there I call another website wiche includes the mailto links an it looks like this `myWebView.loadUrl("WEBSITE"); myWebView.setWebViewClient(new WebViewClient());` Here I also forgot to change `(new WebViewClient())` to `(new MyWebViewClient())`. Now it works, thank you very much madlymad – Tom Hans Apr 12 '13 at 16:10
2

My example:

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

Notice the string array. If you pass only a string it will not be considered.

Emaborsa
  • 2,360
  • 4
  • 28
  • 50
1

Although this is an old question I found none of the answers above worked 100% for me. I want to only open email apps, not text message etc.

I used this code instead, (with no intent.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"));

The extra line: intent.putExtra(Intent.EXTRA_EMAIL, email_address); is so it works on all versions of android.

SammyT
  • 759
  • 1
  • 9
  • 19