Android_WebView

来源:互联网 发布:韦恩图软件 编辑:程序博客网 时间:2024/06/13 02:52
1.webview使用
public class MainActivity extends Activity {

    privateWebView webview;
   @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       webview=(WebView) findViewById(R.id.wv);
       webview.loadUrl("http://www.baidu.com");
       
       webview.setWebChromeClient(new WebChromeClient(){
          @Override
          public void onReceivedTitle(WebView view, Stringtitle) {
             // TODO Auto-generated method stub
             super.onReceivedTitle(view, title);
          }
       });
       //用web自己去调用url,否则会调用系统的浏览器。
       webview.setWebViewClient(new WebViewClient(){
          @Override
          public boolean shouldOverrideUrlLoading(WebViewview, String url) {
             // TODO Auto-generated method stub
             view.loadUrl(url);
             return super.shouldOverrideUrlLoading(view,url);
          }
       });
    }
}
2.自定义title
       webview.setWebChromeClient(new WebChromeClient(){
          @Override
          public void onReceivedTitle(WebView view, Stringtitle) {
             // TODO Auto-generated method stub
             //自定义title
             titleview.setText(title);
             super.onReceivedTitle(view, title);
          }
       });
    点击事件
    classMyLisenter implements View.OnClickListener{

      @Override
      public void onClick(View view) {
         // TODO Auto-generated method stub
         switch (view.getId()) {
         case R.id.update:
            webview.reload();
            Toast.makeText(MainActivity.this, "刷新了",Toast.LENGTH_SHORT).show();
            break;
         case R.id.back:
            webview.goBack();
            //finish();
            break;
         default:
            break;
         }
      }
      
    }
3.使用webview下载
   开启一个下载线程:
public class HttpThread extends Thread{

    privateString mUrl;
    publicHttpThread(String url){
      this.mUrl=url;
    }
      public void run() {
         try {
            URL httpUrl=new URL(mUrl);
            HttpURLConnection conn=(HttpURLConnection)httpUrl.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            InputStream in=conn.getInputStream();
            File downloadFile;
            File sdFile;
            FileOutputStream out=null;
            //判断sd卡状态
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
               downloadFile=Environment.getExternalStorageDirectory();
               sdFile=new File(downloadFile,"test.apk");
               out=new FileOutputStream(sdFile);
            }
            byte[] b=new byte[6*1024];
            int len;
            while ((len=in.read(b))!=-1){
               if(out != null){
                  out.write(b, 0, len);
               }
            }
            if(out!=null){
               out.close();
            }
            if(in != null){
               in.close();
            }
         } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (IOException e) {
            
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }
    下载到SD卡和用系统控件下载:
class MyDownload implements DownloadListener{

      @Override
      public void onDownloadStart(String url, StringuserAgent, String contentDispositoin,
            String mimetype, long contentLength) {
         // TODO Auto-generated method stub
         //判断是否为apk结尾
         if(url.endsWith(".apk")){
            //new HttpThread(url).start();
            //调用系统下载控件下载
            Uri uri=Uri.parse(url);
            Intent intent=newIntent(Intent.ACTION_VIEW,uri);
            startActivity(intent);
         }
         
      }
      
    }
4.网络错误时调用html页面
          //当网络错误是,通过该方法掉用本地的一个页面。
          public void onReceivedError(WebView view, interrorCode,
                String description, String failingUrl) {
             // TODO Auto-generated method stub
             //view.loadUrl("file:///");
             super.onReceivedError(view, errorCode,description, failingUrl);
          }
0 0
原创粉丝点击