Java tutorial
/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.zy.ef.fragment; import android.annotation.TargetApi; import android.content.Context; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import cn.zy.ef.TitleBarActivity; /** * A fragment that displays a WebView. * <p> * The WebView is automically paused or resumed when the Fragment is paused or resumed. */ public class WebViewFragment extends Fragment implements TitleBarActivity.OnBackPressedListener { protected static WebView mWebView; protected static boolean mIsWebViewAvailable; protected String mUrl; protected static FragmentActivity mContext; public WebViewFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mUrl = getArguments().getString(Uri.class.getName()); } mContext = getActivity(); } /** * Called to instantiate the view. Creates and returns the WebView. */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (mWebView != null) { mWebView.destroy(); } mWebView = new WebView(getActivity()); mWebView.setWebChromeClient(new WebChromeClient()); //mWebView.addJavascriptInterface(new InJavaScriptLocalObj(), "androidphone"); configureWebView(mWebView.getSettings()); mIsWebViewAvailable = true; return mWebView; } protected void configureWebView(WebSettings settings) { settings.setJavaScriptCanOpenWindowsAutomatically(true);//js??window.open()false settings.setJavaScriptEnabled(true);//??jsfalsetrue????XSS? settings.setSupportZoom(true);//??true settings.setBuiltInZoomControls(false);//?false settings.setUseWideViewPort(true);//??? settings.setLoadWithOverviewMode(true);//setUseWideViewPort(true) settings.setAppCacheEnabled(true);//? settings.setDomStorageEnabled(true);//DOM Storage settings.setCacheMode(WebSettings.LOAD_DEFAULT); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); //?webviewurl return true; } @Override public void onPageFinished(WebView view, String url) { WebViewFragment.this.onPageFinished(view, url); } }); if (!TextUtils.isEmpty(mUrl)) { mWebView.loadUrl(mUrl); } } public void onPageFinished(WebView view, String url) { // if (view.canGoBack()) { // ((TitleBarActivity) getActivity()).setBackButton(null); // } else { // ((TitleBarActivity) getActivity()).setLeftButtonEmtpy(); // } } /** * Called when the fragment is visible to the user and actively running. Resumes the WebView. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public void onPause() { super.onPause(); ((TitleBarActivity) getActivity()).removeBackPressedListener(this); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { mWebView.onPause(); } } /** * Called when the fragment is no longer resumed. Pauses the WebView. */ @Override public void onResume() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { mWebView.onResume(); } ((TitleBarActivity) getActivity()).addBackPressedListener(this); super.onResume(); } /** * Called when the WebView has been detached from the fragment. The WebView is no longer * available after this time. */ @Override public void onDestroyView() { ((TitleBarActivity) getActivity()).setLeftButtonEmtpy(); mIsWebViewAvailable = false; super.onDestroyView(); } /** * Called when the fragment is no longer in use. Destroys the internal state of the WebView. */ @Override public void onDestroy() { if (mWebView != null) { mWebView.destroy(); mWebView = null; } super.onDestroy(); } /** * Gets the WebView. */ public static WebView getWebView() { return mIsWebViewAvailable ? mWebView : null; } public static Context getmContext() { return mContext; } @Override public boolean onBackPressed() { if (canGoBack()) { mWebView.goBack(); //goBack()WebView? return true; } return false; } private boolean canGoBack() { return mWebView.canGoBack(); } }