2011-10-27 82 views
3
package com.helloworld; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Window; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.view.KeyEvent; 
import android.content.Intent; 
import android.net.MailTo; 
import android.content.Context; 


public class HelloWorld extends Activity 
{ 
    final Activity activity = this; 


    WebView webview; 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (webview != null && (keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { 
     webview.goBack(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.getWindow().requestFeature(Window.FEATURE_PROGRESS); 
     setContentView(R.layout.main); 


     webview = (WebView) findViewById(R.id.webview); 
     webview.getSettings().setJavaScriptEnabled(true); 
     webview.getSettings().setBuiltInZoomControls(true); 
     webview.getSettings().setUseWideViewPort(true); 
     webview.loadUrl("http://www.blahblah.org"); 

     webview.setWebChromeClient(new WebChromeClient() { 
      public void onProgressChanged(WebView view, int progress) 
      { 
       activity.setTitle("Loading..."); 
       activity.setProgress(progress * 100); 

       if(progress == 100) 
        activity.setTitle(R.string.app_name); 
      } 
     }); 

     webview.setWebViewClient(new WebViewClient() { 
      @Override 
      public void onReceivedError(WebView view, int errorCode, String  description, String failingUrl) 
      { 
      // Handle the error 
      } 

    @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) 
     { 
      if(url.startsWith("mailto:")){ 
       MailTo mt = MailTo.parse(url); 
       Intent i = newEmailIntent(activity.this, mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc()); 
       startActivity(i); 
       view.reload(); 
       return true; 
      } 

      view.loadUrl(url); 
      return true; 
     } 

    }); 
} 
    public static Intent newEmailIntent(Context context, String address, String subject, String body, String cc) { 
     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address }); 
     intent.putExtra(Intent.EXTRA_TEXT, body); 
     intent.putExtra(Intent.EXTRA_SUBJECT, subject); 
     intent.putExtra(Intent.EXTRA_CC, cc); 
     intent.setType("message/rfc822"); 
     return intent; 
    } 
} 

上面的代码正常运行,除非单击mailto链接,设备信使打开并且不会将mailto“body”放置在信使的撰写部分中。相同的mailto链接在设备Web浏览器中正常工作。任何想法,将不胜感激。谢谢!android mailto代码无法正常运行

回答

0

我在测试中遇到类似问题, 但事实证明,使用text/plain类型, 它试图发送短信。

我想你想改变两件事情。

intent.setType("text/html"); 

但是你也应该使用Intent.createChooser

startActivity(Intent.createChooser(intent, "Some prompt for the user:")); 

特别是对于电子邮件,他们可能同时安装了android电子邮件应用程序和gmail应用程序。 或者他们可能没有安装能够发送html电子邮件的应用程序。

这解决了我在模拟器上的一个问题,它一直在断开。

以下是我的工作电子邮件发件人的完整代码。

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 

emailIntent.setType("text/html"); 
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, EMAIL_TO); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, EMAIL_SUBJECT); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, EMAIL_TEXT); 

startActivity(Intent.createChooser(emailIntent, "Send Email to the Author:")); 

更新:

再次阅读问题 很可能需要添加一些调试,确保其不邮寄地址解析这是错误的。

String tag = "emaildebug"; 
Log.d(tag, "address: "+address); 
Log.d(tag, "body: "+body); 
Log.d(tag, "subject: "+subject); 
Log.d(tag, "cc:  "+cc); 
+0

我不是肯定究竟如何实现你的代码在上面的代码。我做的一件事就是先用mt.getBody改变newEmailIntent行的顺序。当我运行应用程序时,mailto链接的主体被放置在信使的合成部分,但链接被切断。它看起来像mailto代码不能识别“http”地址和“?”在链接中。 – zef99

+0

我认为你的问题措词错误。你是问题,我猜实际上是因为你的mailto看起来像'mailto:[email protected]?body = some + link + to + http://example.com?query = something '。这当然是行不通的。您需要转义该网址。类似'http%3A%2F%2Fexample.com%3Fquery%3Dsomething' –

+0

我改变了:,?和/使用正确的网址编码。单击webview中的链接时,身体和主体仍然未在Messenger中预填。完全相同的链接在其他浏览器中完美。 – zef99