在Android 7.1.1上我的应用程序上有一个页面,它创建了一个webview并允许在我的网站上进行一些导航。第一页加载正常,但是当我点击webview中的链接时,它会显示一个白色的空白页面。这在以前版本的android上都可以正常工作。
我理解google使用webview进行的更改并且chrome已更新,并且我已经通过各种重置和安装新的webview系统应用程序,因此我不确定问题是否相关。我想它不是第一页不会加载。
这是我的网页视图代码
public class WebViewActivity extends Activity
{
Service details;
String URL;
WebView webView;
private DataManager mDataManager;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
URL = (String)bundle.getString("url");
webView = (WebView) findViewById(R.id.wv);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setWebViewClient(new GeoWebViewClient());
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setGeolocationEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebChromeClient(new GeoWebChromeClient());
webView.clearCache(true);
webView.loadUrl(URL);
//webView.setWebViewClient(new Client());
//webView.setWebChromeClient(new ChromeClient());
if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else if(Build.VERSION.SDK_INT >=11 && Build.VERSION.SDK_INT < 19) {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
/**
* WebChromeClient subclass handles UI-related calls
* Note: think chrome as in decoration, not the Chrome browser
*/
public class GeoWebChromeClient extends WebChromeClient
{
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback)
{
// Always grant permission since the app itself requires location
// permission and the user has therefore already granted it
callback.invoke(origin, true, false);
}
}
public class GeoWebViewClient extends WebViewClient
{
@Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
/*@deprecated
public boolean shouldoverrideurlloading(webview view, string url) {
return false;
}*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
{
view.loadUrl(request.toString());
return true;
}
}
}
我认为这可能与shouldOverrideUrlLoading
答案 0 :(得分:1)
您需要将WebClient
设置为webView
。如果您想在webview
中打开链接。
然后这样做。
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
for Android N上面使用重写方法:
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return false;
}