开发者

how to set anchor tag mailto: attribute in webview, android

开发者 https://www.devze.com 2023-01-05 21:11 出处:网络
I tried to s开发者_如何转开发et anchor tag mailto attribute like <a href=\'mailto:info@company.com\'>info@company.com</a>

I tried to s开发者_如何转开发et anchor tag mailto attribute like

<a href='mailto:info@company.com'>info@company.com</a>

in webview. when i run the app on simulator and click on the link it is showing "Unsupported Action .."

How i can set mailto attribute work out in android webview....

Thanks


WebView does not support advanced HTML tags... what you will have to do is:

  1. Set a webclient to your webview and override the url loading
  2. When you detect a link with mailto try to send the email.

I'm gonna give you a little snippet of code for you to have an idea. Keep in mind this is just a basic example and I can't test it right now:

public void onCreate(Bundle icicle) {
    // blablabla
    WebView webview = (WebView) findViewById(R.id.webview); 
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient( new YourWebClient()); 
    // blablabla
}

private class YourWebClient extends WebViewClient {     
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.contains("mailto")) {
            // TODO: extract the email... that's your work, LOL
            String email = "";
            sendEmail();
            return super.shouldOverrideUrlLoading(view, url);
        }
        view.loadUrl(url);
        return true;
    }
}

Then, send the email:

public void sendEmail(String email){
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{email});

    String mySubject = "this is just if you want";
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, mySubject);
    String myBodyText = "this is just if you want";
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, myBodyText);
    context.startActivity(Intent.createChooser(intent, "Send mail...));
}


set to

myTemplate ="<a>info@company.com</a>";

or just

myTemplate ="info@company.com";

and load into your WebView

mWebView.loadDataWithBaseURL(null, myTemplate, "text/html", "utf-8", null);


Here is another option to Cristian's Answer.

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
  if (!(url.startsWith("http") || url.startsWith("#"))) {
    launchIntent(url);
    return true;
  }
  view.loadUrl(url);
  return true;
}

private void launchIntent(String url){
    final Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
    Uri uri = Uri.parse(url);
    intent.setData(uri);
    context.startActivity(intent);
}

This will allow anchor tags like mailto: tel: and google.navigation:q=an+url+uncoded+address

You may want to adjust the conditional if your html page has other anchors that you do not want an intent to launch with.

0

精彩评论

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