WebView加载电子邮件URL,而不是打开Chooser Intent

时间:2014-06-17 21:22:25

标签: android android-intent webview

如果应用程序的用户选择某个图标,则WebView将加载以' mailto:'开头的网址。我已经包含了一种尝试通过启动选择意图来访问备用电子邮件应用程序来解决此问题的方法,但它不能用于未知原因。任何帮助将不胜感激。

MainActivity.java

package com.example.zangle;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    public WebView student_zangle;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  
        WebView student_zangle = (WebView) findViewById(R.id.student_zangle);
        student_zangle.setWebViewClient( new YourWebClient());
        student_zangle.loadUrl("https://zangleweb01.clovisusd.k12.ca.us/studentconnect/");
        student_zangle.setWebViewClient(new WebViewClient());
        student_zangle.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
        WebSettings settings = student_zangle.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(true);      
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
    }

    public void downloadfileto(String fileurl, String filename) { 
        String exception; 
        try { 
                FileOutputStream f = new FileOutputStream(filename); 
                try { 
                        URL url = new URL(fileurl); 
                        URLConnection urlConn = url.openConnection(); 
                        InputStream is = urlConn.getInputStream(); 
                        BufferedInputStream bis = new BufferedInputStream(is, 8000); 
                        int current = 0; 
                        while ((current = bis.read()) != -1) { 
                                f.write((byte) current); 
                        } 
                } catch (Exception e) { 
                        exception = e.getMessage(); 
                } 
                f.flush(); 
                f.close(); 
        } catch (FileNotFoundException e) { 
                e.printStackTrace(); 
        } catch (IOException e) { 
                e.printStackTrace(); 
        } 
    } 

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        student_zangle = (WebView) findViewById(R.id.student_zangle);
        if ((keyCode == KeyEvent.KEYCODE_BACK) && student_zangle.canGoBack()) {
            student_zangle.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class YourWebClient extends WebViewClient {     
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("mailto")) {
                String mail = url.replaceFirst("mailto:", "");
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("message/rfc822");
                intent.putExtra(Intent.EXTRA_EMAIL, mail );
                return super.shouldOverrideUrlLoading(view, url);
            } 
            view.loadUrl(url);
            return true;
        }
    }
        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});
            *** startActivity(Intent.createChooser( emailIntent, "Send mail..."));
        }

}

更新: 还是行不通.. 更新了YourWebClient类和sendEmail方法

private class YourWebClient extends WebViewClient {     
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("mailto")) {
                String mail = url.replaceFirst("mailto:", "");
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("message/rfc822");
                intent.putExtra(Intent.EXTRA_EMAIL, mail );
                startActivity(intent);
                sendEmail(mail);
                student_zangle.goBack();
                return true;            
                } 
            view.loadUrl(url);
            return true;
        }
    }
        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});
            startActivity(Intent.createChooser( emailIntent, "Select application: "));
        }

1 个答案:

答案 0 :(得分:1)

第一步是删除android:noHistory="true"文件中的AndroidManifest.xml。然后,我删除了sendEmail()方法以及WebViewClient类中对它的调用(否则应用程序会提示用户进行两次活动)。