【发布时间】:2016-08-31 08:06:58
【问题描述】:
我有一个使用webview 的安卓应用。我展示了我的整个网站。我的问题是,当我单击导航上的按钮以加载页面时,有时它们会通过第一次触摸打开,有时没有任何效果。我必须触摸按钮 2 次、3 次或更多次,它们才会打开。
这也适用于网站中的网址。这真是奇怪的行为,我不知道原因。
通过使用pageStart() 和页面完成,我看到它开始了,但是当进度百分比为 10 时,pageFinish() 说它已停止加载!
android studio 模拟器中不存在该问题,仅在真实手机中。 (并且存在于不同的手机和安卓版本中)
如果需要,这是我的代码的一部分。
webView = (WebView) findViewById(R.id.webview1);
webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setAllowFileAccess(true);
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);
}
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.searchAds) {
startWebView("http://www.test.com/");
} else if (id == R.id.myPage) {
startWebView("http://www.test.com/profile");
} else if (id == R.id.allNewPosts) {
startWebView("http://www.test.com/");
} else if (id == R.id.followingsNewPosts) {
startWebView("http://www.test.com/new/");
} else if (id == R.id.followingsNewPosts) {
startWebView("http://www.test.com/categories/");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void startWebView(String myURL) {
if (!isNetworkStatusAvialable(getApplicationContext())) {
Toast.makeText(getApplicationContext(),"No internet access" ,Toast.LENGTH_LONG).show();
} else {
webView.setWebViewClient(new Client());
webView.setWebChromeClient(new ChromeClient());
webView.loadUrl(myURL);
}
}
public class Client extends WebViewClient {
ProgressDialog progressDialog;
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
//If you will not use this method url links are open in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!url.contains("test.com")) {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
view.loadUrl(url);
return true;
}
}
//Show loader on url load
public void onLoadResource(WebView view, String url) {
// Then show progress Dialog
if (progressDialog == null && url.contains("test.com")) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("loading ...");
progressDialog.show();
}
}
// Called when all page resources loaded
public void onPageFinished(WebView view, String url) {
try {
// Close progressDialog
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
progressRunning = false;
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
【问题讨论】:
-
它的发生是因为网络互联网速度或连接,电话速度,当你的速度变慢时,如果速度快,那么它会加载更多时间,然后网页视图数据加载速度很快。有时视图会反映你在 webview 中看不到 Perfect 。因为速度....
-
@amitasharma 它发生的次数太多了!我的意思是我不认为这是因为互联网速度,因为它足够好。即使只有“嗨”消息的页面也无法加载!它将所有这些页面加载到 10% 并完成!如果是因为网速,进度必须变慢:(
-
在您的手机中,您是否在后台检查了一些其他应用程序是否正在运行,如果有其他应用程序正在运行,那么速度可能会变慢......在代码中,您正在将您的 URL 加载到 webview 可能该网址是否有大数据,因此显示需要时间......
-
您能否确认您的互联网连接稳定且没有请求超时?
-
@amitasharma 是的,其他应用程序也使用互联网!像什么应用程序,电报.. 但我的问题是所有页面加载都停止在 10% !当您强硬或触摸哪个页面或链接时没有什么不同!当达到 10 % onPageFinished(WebView view, String url) 函数显示它!
标签: android webview android-websettings