public class WebViewActivity extends BaseActivity implements OnClickListener { private WebView mWebView; private ImageView mBack; private TextView mTitle; private Context mcontext; private Handler handler; private boolean mIsHotActivity = false; @Override protected void onCreate(Bundle arg0) { setContentView(R.layout.oa_webview_layout); mWebView = (WebView) findViewById(R.id.webview); mBack = (ImageView) findViewById(R.id.back); mTitle = (TextView) findViewById(R.id.title); mBack.setOnClickListener(this); Bundle bundle = this.getIntent().getExtras(); final String url = bundle.getString("url"); final String title = bundle.getString("title"); mTitle.setText(title); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); mWebView.getSettings().setDefaultTextEncodingName("utf-8"); mWebView.getSettings().setAllowFileAccess(true); mWebView.getSettings().setAllowFileAccessFromFileURLs(true); mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String loadUrl) { view.loadUrl(loadUrl); return true; } mWebView.loadUrl(url); } switch(msg.what){ case 0: Bundle bundle=msg.getData(); String failingUrl=bundle.getString("FAILINGURL"); mWebView.loadUrl(failingUrl); break; default: break; } } }; super.onCreate(arg0); } //此方法能让返回键返回到上一个页面,而不是直接退出WebViewActivity @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if((keyCode==KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()){ mWebView.goBack(); return true; } else if((keyCode==KeyEvent.KEYCODE_BACK) && !mWebView.canGoBack()){ finish(); } return false; } @Override protected void onPause() { super.onPause(); } private void destoryWebView() { if (mWebView != null) { mWebView.removeAllViews(); mWebView.destroy(); mWebView = null; } } @Override protected void onDestroy() { destoryWebView(); super.onDestroy(); } @Override protected void onStop() { destoryWebView(); super.onStop(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.back: destoryWebView(); finish(); break; default: break; } } }
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦