如何在android webview中处理mailto [HTML]

时间:2017-06-17 10:50:48

标签: android html android-webview mailto

我在HTML页面中有一个href链接,我链接到这样的电子邮件地址:

<p>sometext <a href="mailto:mymailadresse@gmail.com">mymailadresse@gmail.com</a></p>

这是我的Android主要活动:

import android.app.Activity; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient;

import com.google.android.gms.common.api.GoogleApiClient;

public class MainActivity extends Activity {

/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;
private WebView view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    view = (WebView) this.findViewById(R.id.webView);
    view.getSettings().setJavaScriptEnabled(true);
    view.getSettings().setDomStorageEnabled(true);
    view.setWebViewClient(new MyBrowser());
    view.loadUrl("file:///android_asset/www/index.html"); //try js alert
    view.setWebChromeClient(new WebChromeClient()); // adding js alert 


}

/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && view.canGoBack()) {
        view.goBack(); //method goback()
        return true;
    }

    return super.onKeyDown(keyCode, event);
    }
}

我知道我需要对主要活动做些什么,但我不知道是什么?那么somone可以帮助我吗?

提前致谢!

1 个答案:

答案 0 :(得分:5)

在shouldOverrideUrlLoading()方法中使用了这个逻辑

  if (url.startsWith("tel:") || url.startsWith("sms:") || url.startsWith("smsto:") || url.startsWith("mailto:") || url.startsWith("mms:") || url.startsWith("mmsto:")) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
  else {
         view.loadUrl(url);
       }