2010-09-02 50 views
3

我想攔截在我的應用程序中的嵌入式webview中的mailto:鏈接。我所擁有的工作正常,除非用戶在回到應用後按下鏈接時模糊不清。下面是我在做什麼,我WebViewClient如何處理mailto:在android webview

@Override 
public boolean shouldOverrideUrlLoading(WebView view, String url) { 
    if(url.startsWith("mailto:")){ 
     url = url.replaceFirst("mailto:", ""); 
     url = url.trim(); 
     Intent i = new Intent(Intent.ACTION_SEND); 
     i.setType("plain/text").putExtra(Intent.EXTRA_EMAIL, new String[]{url}); 
     context.startActivity(i); 
     return true; 
    } 
    context.findViewById(R.id.loadingBar).setVisibility(View.VISIBLE); 
    view.loadUrl(url); 
    return true; 
} 

如果我做了view.reload()它解決問題,但有一個更好的辦法來解決它不浪費帶寬?我嘗試invalidate(),但它沒有工作。

這裏是我說的一個例子約alt text

+0

模糊?你可以上傳一個例子嗎?我甚至不知道android有一個模糊文本的實用工具。 – QRohlf 2010-09-02 05:42:19

+0

是的,我猜模糊是不好的措辭。謝謝。 – schwiz 2010-09-02 06:55:19

回答

2

這是我的本錢:

if (url.startsWith("mailto:")) { 
    String[] blah_email = url.split(":"); 
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
    emailIntent.setType("text/plain"); 
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{blah_email[1]}); 
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, what_ever_you_want_the_subject_to_be)"); 
    Log.v("NOTICE", "Sending Email to: " + blah_email[1] + " with subject: " + what_ever_you_want_the_subject_to_be); 
    startActivity(emailIntent); 
} 

既然不能在「前」和之後看...看起來好像它正在帶走(或添加)鏈接上的粗體屬性 - 請檢查CSS(可能是JavaScript/Jquery)a:visited並查看它是否包含font-weight: normal;font-weight: bold屬性。

+0

問題並不是試圖發送電子郵件,當您點擊鏈接時,當他們從電子郵件客戶端返回到應用程序時,文本會變得模糊(請參閱上面的圖片)。 – schwiz 2011-03-31 02:14:47

+0

這真的很奇怪 - 你使用的是什麼版本的android?我認爲這是一個頁面特定的問題?我會檢查是否只有1 - 因爲當你點擊鏈接時,它改變了CSS樣式 - 它看起來像是$('#elm_id).css('font-weight','normal')。 ;'(帶走(或添加)粗體屬性) – Wallter 2011-10-19 21:58:31

+1

您也可以使用setType(「application/octet-stream」)或setType(「message/rfc822」)來代替setType(「text/plain」)。這些都沒有真正縮小到電子郵件應用程序,但消息/ rfc822似乎是最接近的。 – 2012-04-04 14:56:36

2

這是一個更復雜的版本,它不使用MailTo類(由於某些原因,它不能正確解析完整的mailto鏈接,只要它們存在,它就會連續地拉電子郵件,抄送,密件抄送,主題和正文。如果它們不存在,它會跳過它並轉到下一個,但是這要求鏈接創建者將所有東西都整理好,如果它失靈,它將不起作用。關心它的次序。

對於那些關心,這也讓直接鏈接到市場應用正常工作。

@Override 
public boolean shouldOverrideUrlLoading(WebView view, String url) { 
    if (url == null) { return false; } 
    if (url.startsWith("market://")) { 
     view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); 
     return true; 
    } 
    if (url.startsWith("mailto:")) { 
     url = url.replaceFirst("mailto:", ""); 
     // 
     String theEmail = "", 
      theEmailCC = "", 
      theEmailBCC = "", 
      theSubject = "", 
      theBody = ""; 
     Boolean hasEmail = true, 
      hasEmailCC = url.contains("&cc="), 
      hasEmailBCC = url.contains("&bcc="), 
      hasSubject = url.contains("&subject="), 
      hasBody = url.contains("&body="); 
     int posEmail = 0, 
      posEmailCC = hasEmailCC ? url.indexOf("&cc=") : 0, 
      posEmailBCC = hasEmailBCC ? url.indexOf("&bcc=") : 0, 
      posSubject = hasSubject ? url.indexOf("&subject=") : 0, 
      posBody = hasBody ? url.indexOf("&body=") : 0; 
     // 
     if  (hasEmail && hasEmailCC) { theEmail = url.substring(posEmail, posEmailCC - posEmail); 
     } else if (hasEmail && hasEmailBCC) { theEmail = url.substring(posEmail, posEmailBCC - posEmail); 
     } else if (hasEmail && hasSubject) { theEmail = url.substring(posEmail, posSubject - posEmail); 
     } else if (hasEmail && hasBody ) { theEmail = url.substring(posEmail, posBody - posEmail); 
     } else if (hasEmail     ) { theEmail = url; 
     } else {        /*theEmail = url;*/ } 

     if  (hasEmailCC && hasEmailBCC) { theEmailCC = url.substring(posEmailCC, posEmailBCC - posEmailCC); 
     } else if (hasEmailCC && hasSubject) { theEmailCC = url.substring(posEmailCC, posSubject - posEmailCC); 
     } else if (hasEmailCC && hasBody ) { theEmailCC = url.substring(posEmailCC, posBody - posEmailCC); 
     } else if (hasEmailCC    ) { theEmailCC = url.substring(posEmailCC); 
     } else {        /*theEmailCC = url.substring(posEmailCC);*/ } 
     theEmailCC = theEmailCC.replace("&cc=", ""); 

     if  (hasEmailBCC && hasSubject) { theEmailBCC = url.substring(posEmailBCC, posSubject - posEmailBCC); 
     } else if (hasEmailBCC && hasBody ) { theEmailBCC = url.substring(posEmailBCC, posBody - posEmailBCC); 
     } else if (hasEmailBCC    ) { theEmailBCC = url.substring(posEmailBCC); 
     } else {        /*theEmailBCC = url.substring(posEmailBCC);*/ } 
     theEmailBCC = theEmailBCC.replace("&bcc=", ""); 

     if  (hasSubject && hasBody ) { theSubject = url.substring(posSubject, posBody - posSubject); 
     } else if (hasSubject    ) { theSubject = url.substring(posSubject); 
     } else {        /*theSubject = url.substring(posSubject);*/ } 
     theSubject = theSubject.replace("&subject=", ""); 

     if  (hasBody     ) { theBody  = url.substring(posBody); 
     } else {        /*theBody  = url.substring(posBody);*/ } 
     theBody = theBody.replace("&body=", ""); 

     theSubject = theSubject.replace("%20", " "); 
     theBody = theBody.replace("%20", " ").replace("%0A", "\n"); 
     // 
     Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
     emailIntent.setType("message/rfc822"); 
     // 
     emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { theEmail, }); 
     if (hasEmailCC) { emailIntent.putExtra(android.content.Intent.EXTRA_CC, theEmailCC); } 
     if (hasEmailBCC) { emailIntent.putExtra(android.content.Intent.EXTRA_BCC, theEmailBCC); } 
     if (hasSubject) { emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, theSubject); } 
     if (hasBody) { emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, theBody); } 
     // 
     view.getContext().startActivity(emailIntent); 
     // 
     return true; 
    } 
    return false; 
} 
6

這裏是一個更強大詹姆斯格雷的答案。 應該處理多個地址(逗號分隔)和多個 '抄送'/ 'BCC' 參數:

@Override 
public boolean shouldOverrideUrlLoading(WebView view, String url) { 

    if (url == null) { 
    return false; 
    } 
    if (url.startsWith("market://")) { 
    view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); 
    return true; 
    } 
    if (url.startsWith("mailto:")) { 

    try { 
     List<String> to = new ArrayList<String>(); 
     List<String> cc = new ArrayList<String>(); 
     List<String> bcc = new ArrayList<String>(); 
     String subject = null; 
     String body = null; 

     url = url.replaceFirst("mailto:", ""); 

     String[] urlSections = url.split("&"); 
     if (urlSections.length >= 2) { 

     to.addAll(Arrays.asList(urlSections[0].split(","))); 

     for (int i = 1; i < urlSections.length; i++) { 
      String urlSection = urlSections[i]; 
      String[] keyValue = urlSection.split("="); 

      if (keyValue.length == 2) { 
      String key = keyValue[0]; 
      String value = keyValue[1]; 

      value = URLDecoder.decode(value, "UTF-8"); 

      if (key.equals("cc")) { 
       cc.addAll(Arrays.asList(url.split(","))); 
      } 
      else if (key.equals("bcc")) { 
       bcc.addAll(Arrays.asList(url.split(","))); 
      } 
      else if (key.equals("subject")) { 
       subject = value; 
      } 
      else if (key.equals("body")) { 
       body = value; 
      } 
      } 
     } 
     } 
     else { 
     to.addAll(Arrays.asList(url.split(","))); 
     } 

     Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
     emailIntent.setType("message/rfc822"); 

     String[] dummyStringArray = new String[0]; // For list to array conversion 
     emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, to.toArray(dummyStringArray)); 
     if (cc.size() > 0) { 
     emailIntent.putExtra(android.content.Intent.EXTRA_CC, cc.toArray(dummyStringArray)); 
     } 
     if (bcc.size() > 0) { 
     emailIntent.putExtra(android.content.Intent.EXTRA_BCC, bcc.toArray(dummyStringArray)); 
     } 
     if (subject != null) { 
     emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); 
     } 
     if (body != null) { 
     emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body); 
     } 
     view.getContext().startActivity(emailIntent); 

     return true; 
    } 
    catch (UnsupportedEncodingException e) { 
     /* Won't happen*/ 
    } 

    } 
    return false; 
} 
+0

感謝它幫助我。 – 2015-01-05 06:32:15

+0

這肯定比我當時寫的更接近我現在寫的東西,我對編程有了更多的瞭解。感謝您抽出時間,我沒有。 – 2015-10-02 23:14:44

+0

我會改變行分裂郵件上的「?」太。 例如, - > String [] urlSections = url.split(「\\?| &"); – 2016-03-09 00:20:38