简体   繁体   中英

“http” and “?” are getting cutoff in android webview mailto links

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

    // Don't create another webview reference here,
    // just use the one you declared at class level.
    webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("http://www.example.com");

    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(HelloWorld.this, mt.getBody(), mt.getSubject());
              startActivity(i);
              view.reload();
              return true;
        }

        view.loadUrl(url);
        return true;
    }

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

Trying to add a mailto to a webview application. When you run the application and click a mailto link it opens up the messenger. For some reason "http" and "?" are getting cut off and not recognized by the mailto. The same mailto link works perfect in devices normal browser. The only field I need to get are subject and body.

Good bet: Make sure any of the content in the mailto link subject/body fields are URL escaped (no literal slashes, ampersands, etc.) when they get to the MailTo parsing code. " ? " in particular is a reserved character for headers in the Mailto RFC .

That is, not

mailto://...subject=http://foo.com?x=y

but instead try

mailto://...subject=http%3A%2F%2Ffoo.com%3Fx%3Dy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM