简体   繁体   中英

Opening tel: & mailto: links in Android WebViewClient

I have been looking for an answer to this question that I can understand for the last couple of days. After trying all of the code snippets online that I have seen I am still having difficulties. I am very new to the android sdk and java, actually this is my first shot at writting an Android app. So my question is this, how come I keep getting the ever so famous error "Page cant be displayed" when clicking on a mailto or tel link ?

Here is my code:

package com.mine.mobile;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MineActivity extends Activity {
/** Called when the activity is first created. */
/**@Override */
WebView webview;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    webview = (WebView) findViewById(R.id.webview);
    webview.setWebViewClient(new WebViewClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("http://my.mobilesite.com");
    }

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("tel:")) {
        startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
        return true;
    } else if (url.startsWith("mailto:")) {
        url = url.replaceFirst("mailto:", "");
        url = url.trim();
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("plain/text").putExtra(Intent.EXTRA_EMAIL, new String[]{url});
        startActivity(i);
        return true;
    } else if (url.startsWith("geo:")) {
        Intent searchAddress = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
        startActivity(searchAddress); 
}
        else {
        view.loadUrl(url);
        return true;
    }
    return true;
}
}

You put the method in your Activity. It needs to override the method in WebView. For example:

    webview.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
          ...
        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM