2014-06-17 68 views
4

如果應用程序的用戶選擇某個圖標,則WebView將加載以「mailto:」開頭的url。我已經加入了一個方法,試圖通過啓動一個選擇意向來訪問另一個電子郵件應用程序來解決這個問題,但它不能用於未知的原因。任何幫助將不勝感激。WebView加載電子郵件地址,而不是打開Chooser Intent

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: 仍然沒有工作.. 更新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: ")); 
     } 
+0

你永遠不會在你提供給我們的代碼中調用sendEmail(),並且你永遠不會以你在shouldOverrideUrlLoading()中創建的意圖調用startActivity()() – panini

+0

在活動中的哪個位置我會調用sendEmail()?在shouldOverrideUrlLoading()或onCreate()中? – frgnvola

回答

1

的第一步是在我AndroidManifest.xml要刪除的文件android:noHistory="true"。然後,我刪除了sendEmail()方法以及WebViewClient類中的調用方法(否則應用程序會提示用戶進行兩次活動)。

相關問題