Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

android - Webview email link (mailto)

I have a view and view the site has malito code to send email. When I open the link opens in an error. I want that when I open the link opens Gmail app or another email application. Thanks to all helpers.

public class teacher extends Activity implements OnClickListener {
    WebView webView;
    final Activity activity = this;    
    @Override
    public void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.teacher);
        webView = (WebView) findViewById(R.id.webView145);    
        webView.getSettings().setJavaScriptEnabled(true);           webView.loadUrl("https://dl.dropboxusercontent.com/u/233211/%D7%A8%D7%A9%D7%99%D7%95%D7%9F/iWebKit%20demo/ther.html");    
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);    
                if (progress == 100)
                activity.setTitle(R.string.app_name); 
            }       });    
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
            // Handle the error
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;            }           });}
        public void onBackPressed() {    
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            Intent B = new Intent(this, MainActivity.class);
            startActivity(B);
        }}    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (webView.canGoBack() == true) {
                    webView.goBack();
                } else {
                    finish();
            }return true;}          }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }}
question from:https://stackoverflow.com/questions/18874282/webview-email-link-mailto

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to create a subclass of WebViewClient and override mailto URL loading. Example:

public class MyWebViewClient extends WebViewClient {
  private final WeakReference<Activity> mActivityRef;

  public MyWebViewClient(Activity activity) {
    mActivityRef = new WeakReference<Activity>(activity);
  }

  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("mailto:")) {
      final Activity activity = mActivityRef.get();
      if (activity != null) {
        MailTo mt = MailTo.parse(url);
        Intent i = newEmailIntent(activity, mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc());
        activity.startActivity(i);
        view.reload();
        return true;
      }
    } else {
      view.loadUrl(url);
    }
    return true;
  }

  private Intent newEmailIntent(Context context, String address, String subject, String body, String cc) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address });
    intent.putExtra(Intent.EXTRA_TEXT, body);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_CC, cc);
    intent.setType("message/rfc822");
    return intent;
  }
}

Then you have to set this custom WebViewClient to your WabView:

webView.setWebViewClient(new MyWebViewClient(activity);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...