简体   繁体   English

无法获取shouldOverrideUrlLoading以在Android应用中工作

[英]Cannot get shouldOverrideUrlLoading to work in Android app

I have made a small app based on WebView . 我已经制作了一个基于WebView的小型应用程序。 I would like to direct all links except for the domain in myWebView.loadUrl() to a browser instead of them being opened in WebView . 我想将除myWebView.loadUrl()的域以外的所有链接myWebView.loadUrl()到浏览器,而不是在WebView中打开它们。 And I would like to direct mailto:// links to the user's mail program. 我想将mailto://链接指向用户的邮件程序。

I have tried several examples I have found using shouldOverrideUrlLoading() but each time I end up with errors or it is not working. 我已经尝试过使用shouldOverrideUrlLoading()找到的几个示例,但是每次我shouldOverrideUrlLoading()错误或无法正常工作时,都可以使用。 I am a total noob and not able to understand and much less correct the errors - my abilities are limited to copy and paste. 我是一个菜鸟,不了解并且更不能纠正错误-我的能力仅限于复制和粘贴。

Can anyone help me with the necessary code and where to place it in the code below? 谁能帮我提供必要的代码,以及将其放置在下面的代码中?

package dk.ugenshoroskop.mobil;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class UgensHoroskop extends Activity {
private WebView myWebView;

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ugens_horoskop);

    myWebView = (WebView) findViewById(R.id.webview);
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.loadUrl("http://ugens-horoskop.dk/mobile.php");
    myWebView.setWebViewClient(new WebViewClient());
    }     

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

After a lot of trial and error, I have succeeded in making http links work, you will find the solution below. 经过大量的试验和错误,我成功地使http链接起作用,您将在下面找到解决方案。 However, mailto links are still not working. 但是,mailto链接仍然不起作用。 When I click a mailto link I am told the action is not currently supported. 当我单击mailto链接时,系统会通知我当前不支持该操作。 Any ideas for getting mailto to work would be appreciated. 任何使mailto正常工作的想法将不胜感激。

This is what I did: 这是我所做的:

package dk.ugenshoroskop.mobil;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.net.Uri;                  /* new in order to handle URIs */

public class UgensHoroskop extends Activity {
private WebView myWebView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ugens_horoskop);

myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://ugens-horoskop.dk/mobile.php");
myWebView.setWebViewClient(new WebViewClient()     /* inserted code: */
{  
@Override  
public boolean shouldOverrideUrlLoading(WebView view, String url)  
{  
if (url.contains("http://ugens-horoskop.dk"))  {
myWebView.loadUrl(url);  
return false; } 
else {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}  
}  
}

/* end inserted code */
);
}     

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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