Android WebView使用

WebActivity

package com.android.xiong.gridlayoutTest;




import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;


public class WebActivity extends Activity implements OnClickListener {
/**
* @author zm
*/
private TextView tv_titlle;
private Button btn_back;
private Button btn_refresh;
private WebView wv;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
wv = (WebView) findViewById(R.id.webView1);
btn_back = (Button) findViewById(R.id.btn_back);
btn_refresh = (Button) findViewById(R.id.btn_refresh);
tv_titlle = (TextView) findViewById(R.id.tv_title);

               //设置监听事件
btn_back.setOnClickListener(this);
btn_refresh.setOnClickListener(this);



wv.setDownloadListener(new MyDownload());

                  // 加载url

wv.loadUrl("http://sj.360.cn/index.html");
wv.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
// 将标题显示在TextView中
tv_titlle.setText(title);
super.onReceivedTitle(view, title);
}
});
wv.setWebViewClient(new WebViewClient() {


@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// 再次加载url
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
});


}


class MyDownload implements DownloadListener {


@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype, long contentLength) {
// TODO Auto-generated method stub
System.out.println("-------" + url);
//如果以.apk文件结尾进行下载
if (url.endsWith(".apk")) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
// new HttpThread(url).start();
}
}


}


@Override
public void onClick(View v) {
// 根据id响应不同的事件
switch (v.getId()) {
case R.id.btn_back:
finish();
break;
case R.id.btn_refresh:
wv.reload();
break;


default:
break;
}


}



}

HttpThread

package com.android.xiong.gridlayoutTest.http;


import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


import android.os.Environment;


public class HttpThread extends Thread {
private String murl;


/**
* zm
* 
*/
public HttpThread(String url) {


super();
this.murl = url;
}


@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {

                //开始下载
            System.out.println("start download......");
URL url = new URL(murl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
//conn.setDoOutput(true);
//获取输入流
InputStream is = conn.getInputStream();
File downloadFile;
FileOutputStream fos = null;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
downloadFile = Environment.getExternalStorageDirectory();
File sdcardFile = new File(downloadFile, "test.apk");
fos = new FileOutputStream(sdcardFile);
byte[] bytes = new byte[6*1024];
int len = 0;
while ((len = is.read(bytes)) != -1) {
if (fos != null) {
fos.write(bytes, 0, len);
}


}

                                //关闭流
if (fos != null) {
fos.close();
}
if (is != null) {
is.close();
}
System.out.println("download success!");
}
} catch (Exception e) {
// TODO: handle exception
}
}


}

activity_web.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2" >


        <Button
            android:id="@+id/btn_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="返回" />


        <Button
            android:id="@+id/btn_refresh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="刷新" />
        
    </LinearLayout>


    <WebView
        android:id="@+id/webView1"
        android:layout_marginTop="20dp"
        android:padding="20dp"
        android:layout_width="match_parent"
        android:layout_height="800dp" />
    <TextView 
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="显示的信息"
        android:gravity="center"
        android:typeface="serif"
        
        />


</LinearLayout>

需要注意的还需要在AndroidManifest.xml配置两个权限网络和读取SD卡权限

 

  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

效果如下图所示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值