2010-09-02 4 views
3

J'essaie d'intercepter mailto: liens dans un webview intégré dans mon application. Ce que j'ai fonctionne bien, sauf lorsque l'utilisateur appuie sur le lien, il est flou lors du retour à l'application. Voici ce que je fais dans mon WebViewClientComment gérer mailto: dans 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; 
} 

Si je fais ce fait view.reload() résoudre le problème, mais est-il une meilleure façon de le réparer sans perdre la bande passante? J'ai essayé d'invalider() mais cela n'a pas fonctionné.

ici est un exemple de ce dont je parle alt text

+0

floue? pouvez-vous télécharger un exemple? Je ne savais même pas que Android avait un utilitaire pour flouter le texte. – QRohlf

+0

oui, je suppose que le flou est mauvais libellé. Merci. – schwiz

Répondre

2

Voici ce que j'ai:

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

Depuis que je ne vois pas le « avant » et après ... Il semble que cela supprime (ou ajoute) l'attribut en gras sur le lien - vérifiez le CSS (peut-être le JavaScript/Jquery) pour a:visited et voyez s'il contient des attributs font-weight: normal; ou font-weight: bold.

+0

le problème n'est pas d'essayer d'envoyer l'email, c'est que lorsque vous cliquez sur le lien, le texte devient flou (voir photo ci-dessus) quand ils reviennent à l'application du client de messagerie. – schwiz

+0

C'est vraiment étrange - quelle version d'android utilisiez-vous? Est-ce que c'est un problème spécifique à une page? Je vérifierais s'il y a seulement 1 là - parce que quand vous cliquez sur le lien il change le style de css - il semble que c'est $ ('# elm_id) .css (' font-weight ',' normal ') ; '(supprimer (ou ajouter) l'attribut gras) – Wallter

+1

Vous pouvez également utiliser setType (" application/octet-stream ") ou setType (" message/rfc822 ") au lieu de setType (" text/plain "). Aucun de ceux-ci ne le limite vraiment aux applications d'email seulement mais le message/rfc822 semble venir le plus proche. –

2

Voici une version plus complexe qui n'utilise pas la classe MailTo (qui, pour une raison ou une autre, n'analyserait pas correctement un lien mailto complet.) Il tira successivement mail, cc, bcc, subject et body tant qu'ils existent Si elles n'existent pas, il les saute et passe à la suivante, ce qui nécessite que le créateur du lien mette tout en ordre, si cela ne fonctionne pas, je peux en faire un autre plus tard. soins quel ordre il se trouve.

Pour ceux que les soins, cela permet également des liens directs vers des applications du marché fonctionnent aussi bien.

@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

Voici un plus robuste version de la réponse de James Gray. Il convient de manipuler plusieurs adresses (séparées par des virgules) et de multiples 'cc'/paramètres 'Cci':

@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

remercie son aide moi. –

+0

C'est certainement mieux que ce que j'ai écrit à l'époque et plus proche de ce que j'écrirais maintenant que j'ai une tonne de connaissances sur la programmation. Merci d'avoir pris le temps que je n'ai pas. –

+0

Je voudrais changer la ligne pour diviser le mailto sur "?" aussi. par exemple, -> String [] urlSections = url.split ("\\? | &"); –