本文實例講述了Android編程使用WebView實現文件下載功能的兩種方法。分享給大家供大家參考,具體如下:
在應用中,通常會使用到文件下載功能,一般我們都是寫一個下載操作工具類,在異步任務中執行下載功能。
今天我們來看下如何使用WebView的文件下載功能!
方法1,自定義下載操作
1. 先來布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:ldm="http://schemas.android.com/apk/res/com.ldm.learn"android:layout_width="match_parent"android:layout_height="match_parent"><WebViewandroid:id="@+id/test_wv"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_margin="15dp" /></RelativeLayout>
2. 實現自定義下載工具操作異步線程類:
public class DownLoadThread extends Thread {private String downLoadUrl;private Context context;private FileOutputStream out = null;private File downLoadFile = null;private File sdCardFile = null;private InputStream in = null;public DownLoadThread(String downLoadUrl, Context context) {super();this.downLoadUrl = downLoadUrl;this.context = context;}@Overridepublic void run() {try {URL httpUrl = new URL(downLoadUrl);HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setDoInput(true);// 如果打算使用 URL 連接進行輸入,則將 DoInput 標志設置為 true;如果不打算使用,則設置為 false。默認值為 true。conn.setDoOutput(true);// 如果打算使用 URL 連接進行輸出,則將 DoOutput 標志設置為 true;如果不打算使用,則設置為 false。默認值為 false。in = conn.getInputStream();if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Toast.makeText(context, "SD卡不可用!", Toast.LENGTH_SHORT).show();return;}downLoadFile = Environment.getExternalStorageDirectory();sdCardFile = new File(downLoadFile, "download.apk");out = new FileOutputStream(sdCardFile);byte[] b = new byte[1024];int len;while ((len = in.read(b)) != -1) {out.write(b, 0, len);}if (out != null) {out.close();}if (in != null) {in.close();}}catch (Exception e) {e.printStackTrace();} }}3. 文件下載
public class MainActivity extends Activity {private WebView test_wv;private String downLoadUrl = "http://as.baidu.com/a/rank?cid=101&s=1&f=web_alad"; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);this.test_wv = (WebView) findViewById(R.id.test_wv);test_wv.loadUrl(downLoadUrl);test_wv.setWebViewClient(new WebViewClient() {@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);return super.shouldOverrideUrlLoading(view, url);}});//要實現WebView文件下載,實現這個監聽就oktest_wv.setDownloadListener(new DownloadListener() {@Overridepublic void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {Log.v("ldm", url);if (url.endsWith(".apk")) {//判斷是否是.apk結尾的文件路徑new DownLoadThread(url, MainActivity.this).start();}}});}}方法2:通過系統自身下載方式下載(會在通知欄顯示下載進度條)
只需要把這個方法改寫如下:
test_wv.setDownloadListener(new DownloadListener() {@Overridepublic void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {Log.v("ldm", url);Uri uri=Uri.parse(url);Intent intent=new Intent(Intent.ACTION_VIEW, uri);startActivity(intent);}});希望本文所述對大家Android程序設計有所幫助。
新聞熱點
疑難解答