我在我的應用程序中創建了一個WebView
,我明顯地顯示了帶有一些HTML鏈接的網頁。如何攔截WebView中的onPackPressed()?
但是,當我訪問這些鏈接之一,然後點擊返回鍵,我不會返回到調用頁面,而是退出WebView
。
是否有可能改變這種行爲?
我創建了WebView
,如下所示。
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Configuration conf = getActivity().getResources().getConfiguration();
String locale = conf.locale.getLanguage();
WebView webView = (WebView) view.findViewById(R.id.wbInfoSiteWebView);
progressBar = ProgressDialog.show(getActivity(), site.getMainActivity().getResources().getString(R.string.label_bt_info), getArguments().getString("site_name"));
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
Log.i(Constants.APP_TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent intent;
if(url.startsWith("tel:")) {
intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
} else if (url.startsWith("http://maps")) { //http://maps.google.fr/maps?f=q&q=45.772962,4.856504
String location = null;
try {
location = "geo:" + url.substring(url.indexOf("&q=") + 3) + "?q=" + url.substring(url.indexOf("&q=") + 3) + " (" + URLEncoder.encode(getArguments().getString("site_name"), "UTF-8") + ")";
} catch (UnsupportedEncodingException e) {
location = "geo:" + url.substring(url.indexOf("&q=") + 3) + "?q=" + url.substring(url.indexOf("&q=") + 3);
}
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(location));
startActivity(intent);
return true;
} else if (url.startsWith("intent://")) {
MainActivity.intentParams = url.substring(0, url.indexOf("#"));
getActivity().getFragmentManager().popBackStackImmediate("Fragment_mainmap", 0);
return false;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
String url = null;
url = site.get2ndGuideSiteInfosSvce() + "?language=" + locale + "&site_id=" + site_id.toString();
webView.loadUrl(url);
webView.getSettings().setJavaScriptEnabled(true);
}
很抱歉這麼晚纔回復。你的解決方案完美的作品thansk很多。 – 2ndGAB
你真棒。救了我吧 –