开发者

Opening tel: & mailto: links in Android WebViewClient

开发者 https://www.devze.com 2023-04-13 08:09 出处:网络
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

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) {
          ...
        }

}

0

精彩评论

暂无评论...
验证码 换一张
取 消