Android webview problem
Jeg har lavet en simpel android webview app. Den virker fint som den skal, bortset fra at den åbner eksterne links i webview også. Jeg vil gerne have, så den åbner alle eksterne links i default webbrowser, men alle links på mit domain skal forblive i webview..Jeg har fundet frem til denne stump kode:
myWebView.setWebViewClient(new WebViewClient());
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("m.akvariefisk.dk/")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
*********** Her er hele .java filen ************
package com.akvariefisk.app;
import android.net.Uri;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.*;
public class Akvariefisk extends Activity {
private static final String TAG = "Akvariefisk.dk";
private WebView webView;
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_akvariefisk);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
if (savedInstanceState == null)
{
webView.loadUrl("http://m.akvariefisk.dk");
}
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient());
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("m.akvariefisk.dk/")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
@Override
protected void onStart()
{
super.onStart();
Log.i(TAG, "onStart");
}
@Override
protected void onRestart()
{
super.onRestart();
Log.i(TAG, "onRestart");
}
@Override
protected void onPause()
{
super.onPause();
Log.i(TAG, "onPause");
}
@Override
protected void onResume()
{
super.onResume();
Log.i(TAG, "onResume");
}
@Override
protected void onDestroy()
{
super.onDestroy();
Log.i(TAG, "onDestroy");
}
@Override
protected void onStop()
{
super.onStop();
Log.i(TAG, "onStop");
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
webView.saveState(outState);
Log.i(TAG, "onSaveInstanceState");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
webView.restoreState(savedInstanceState);
Log.i(TAG, "onRestoreInstanceState");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_akvariefisk, menu);
return true;
}
}