简体   繁体   中英

Cannot get shouldOverrideUrlLoading to work in Android app

I have made a small app based on 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 . And I would like to direct mailto:// links to the user's mail program.

I have tried several examples I have found using shouldOverrideUrlLoading() but each time I end up with errors or it is not working. 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. However, mailto links are still not working. When I click a mailto link I am told the action is not currently supported. Any ideas for getting mailto to work would be appreciated.

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

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