码迷,mamicode.com
首页 > Web开发 > 详细

WebView

时间:2015-09-22 18:14:06      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

 
声明具有INTERNET权限
 <uses-permission android:name= "android.permission.INTERNET" />
 
 
 
 
private String url="http://stareblankly.cn";
        private WebView webView ;
        private ProgressDialog progressDialog ;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
              setContentView(R.layout. activity_main);
              Uri uri=Uri. parse(url);
              Intent intent= new Intent(Intent.ACTION_VIEW,uri);
              startActivity(intent);
              init();
              
       }
        private void init() {
               webView=(WebView) findViewById(R.id. webView);
               webView.loadUrl( "file:///android_asset/az.html" );      //加载本地资源
               webView.loadUrl( "http://www.baidu.com"); //加载网络资源
               //覆盖webview默认通过第三方或者是系统浏览器打开网页的行为,使得网页可以在 webview中打开。
               webView.setWebViewClient( new WebViewClient(){
                      @Override
                      public boolean shouldOverrideUrlLoading(WebView view, String url) {
                            // 返回值为true的时候控制网页在 webview中打开,为false网络则在系统或者第三方浏览器中打开
                           view.loadUrl(url);
                            return true ;
                     }
                      //webviewClient帮助webview去处理一下页面控制和请求通知
              });
               //启用支持JS
              WebSettings settings= webView.getSettings();
              settings.setJavaScriptEnabled( true);
               //webview 加载页面优先使用缓存加载
              settings.setCacheMode(WebSettings. LOAD_CACHE_ELSE_NETWORK);
               webView.setWebChromeClient( new WebChromeClient(){
                      @Override
                      public void onProgressChanged(WebView view, int newProgress) {
                            if(newProgress==100){
                                   //网页加载完毕
                                  closeDialog();
                           } else{
                                   //网页正在加载,打开progressDialog
                                  openDialog(newProgress);
                           }
                     }
 
                      private void closeDialog() {
                            if(progressDialog !=null&&progressDialog.isShowing()){
                                   progressDialog.dismiss();
                                   progressDialog=null ;
                           }
                           
                     }
 
                      private void openDialog(int newProgress) {
                            if(progressDialog ==null){
                                   progressDialog=new ProgressDialog(MainActivity.this );
                                   progressDialog.setProgressStyle(progressDialog .STYLE_HORIZONTAL);
                                   progressDialog.setProgress(newProgress);
                                   progressDialog.show();
                           } else{
                                   progressDialog.setProgress(newProgress); //刷新进度
                           }
                           
                     }
              });
       }
 
       
        //改写物理按钮--返回按钮的逻辑
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
               if(keyCode==KeyEvent.KEYCODE_BACK){
                      if(webView .canGoBack()){
                            webView.goBack();     //返回上一页
                            return true ;
                     } else{
                           System. exit(0);             //退出程序
                     }
              }
               return super .onKeyDown(keyCode, event);
       }
       

WebView

标签:

原文地址:http://www.cnblogs.com/stareblankly/p/4829220.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!