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代碼無法正常運行
我不是肯定究竟如何實現你的代碼在上面的代碼。我做的一件事就是先用mt.getBody改變newEmailIntent行的順序。當我運行應用程序時,mailto鏈接的主體被放置在信使的合成部分,但鏈接被切斷。它看起來像mailto代碼不能識別「http」地址和「?」在鏈接中。 – zef99
我認爲你的問題措詞錯誤。你是問題,我猜實際上是因爲你的mailto看起來像'mailto:[email protected]?body = some + link + to + http://example.com?query = something '。這當然是行不通的。您需要轉義該網址。類似'http%3A%2F%2Fexample.com%3Fquery%3Dsomething' –
我改變了:,?和/使用正確的網址編碼。單擊webview中的鏈接時,身體和主體仍然未在Messenger中預填。完全相同的鏈接在其他瀏覽器中完美。 – zef99