WebView downloads email instead of opening Chooser Intent

If the application user selects a specific icon, WebView loads a URL starting with 'mailto:'. I have included a method that attempts to fix this by running a choice to access an alternate email application, but it does not work for an unknown reason. Any help would be greatly appreciated.

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..."));
        }

}

UPDATE: Still not working .. YourWebClient class and sendEmail method updated

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: "));
        }
+4
source share
1 answer

android:noHistory="true" AndroidManifest.xml. sendEmail(), WebViewClient ( ).

+1

Source: https://habr.com/ru/post/1544990/


All Articles