我正在开发一个使用webview的应用程序。一切都工作正常,但当我尝试打开webview内部的外部链接时,它会冻结或崩溃。这发生在Android 4.4及以下版本,但在Android 5.0及更高版本上运行良好。
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
///initialize webview
private WebView mwebview;
ProgressBar bar;
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mwebview = (WebView) findViewById(R.id.myWebview);
bar = (ProgressBar) findViewById(R.id.progressBar2);
WebSettings webSettings = mwebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mwebview.loadUrl("http://stalls.co.ke/shopping");
// improve webview perfomance
mwebview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
if (Build.VERSION.SDK_INT >= 19) {
mwebview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
mwebview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
mwebview.getSettings().setAppCacheEnabled(true);
mwebview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
//force links to open in webview only
mwebview.setWebViewClient(new MyWebViewClient());
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the camera action
mwebview.loadUrl("http://stalls.co.ke/shopping");
} else if (id == R.id.nav_gifts) {
mwebview.loadUrl("http://stalls.co.ke/gifts-coupons");
} else if (id == R.id.nav_men) {
mwebview.loadUrl("http://stalls.co.ke/stallswear-men");
} else if (id == R.id.nav_women) {
mwebview.loadUrl("http://stalls.co.ke/stallswear-women");
} else if (id == R.id.nav_kids) {
mwebview.loadUrl("http://stalls.co.ke/stallswear-kids-baby");
} else if (id == R.id.nav_men_cosmetics) {
mwebview.loadUrl("http://stalls.co.ke/copy-of-stalls-cosmetics");
} else if (id == R.id.nav_women_cosmetics) {
mwebview.loadUrl("http://stalls.co.ke/copy-of-men-footwear");
} else if (id == R.id.nav_kids_cosmetics) {
mwebview.loadUrl("http://stalls.co.ke/copy-of-stalls-cosmetics");
} else if (id == R.id.nav_homes) {
mwebview.loadUrl("http://stalls.co.ke/home-kitchen-products");
} else if (id == R.id.nav_garden) {
mwebview.loadUrl("http://stalls.co.ke/garden-products");
} else if (id == R.id.nav_smarthomes) {
mwebview.loadUrl("http://stalls.co.ke/smart-home-products");
} else if (id == R.id.nav_eatery) {
mwebview.loadUrl("http://stalls.co.ke/stallseatery");
} else if (id == R.id.nav_customer_service) {
mwebview.loadUrl("http://stalls.co.ke/help");
} else if (id == R.id.nav_about_us) {
mwebview.loadUrl("http://stalls.co.ke/about-us");
} else if (id == R.id.nav_kijo) {
mwebview.loadUrl("http://stalls.co.ke/house-of-kinjo");
} else if (id == R.id.nav_Airi) {
mwebview.loadUrl("http://stalls.co.ke/Airi");
} else if (id == R.id.nav_mamapeter) {
mwebview.loadUrl("http://stalls.co.ke/house-of-kinjo");
} else if (id == R.id.nav_swahili) {
mwebview.loadUrl("http://stalls.co.ke/swahili-princess");
} else if (id == R.id.nav_allstalls) {
mwebview.loadUrl("http://stalls.co.ke/kenyan-stores");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public Action getIndexApiAction() {
Thing object = new Thing.Builder()
.setName("Main Page") // TODO: Define a title for the content shown.
// TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
AppIndex.AppIndexApi.end(client, getIndexApiAction());
client.disconnect();
}
// go to previous page when back button is pressed
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mwebview.canGoBack()) {
mwebview.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
private class MyWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
bar.setVisibility(view.GONE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mwebview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.loadUrl(request.getUrl().toString());
}
return false;
}
});
} else {
mwebview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
return false;
}
}
}
这是我的MainActivity.java
文件,我想知道为什么会这样。
答案 0 :(得分:0)
尝试使用WebChromeClient;而不是WebViewClient;看看它是否有效。