Android編程使用WebView實現(xiàn)文件下載功能的兩種方法
本文實例講述了Android編程使用WebView實現(xiàn)文件下載功能的兩種方法。分享給大家供大家參考,具體如下:
在應(yīng)用中,通常會使用到文件下載功能,一般我們都是寫一個下載操作工具類,在異步任務(wù)中執(zhí)行下載功能。
今天我們來看下如何使用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" > <WebView android:id="@+id/test_wv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="15dp" /> </RelativeLayout>
2. 實現(xiàn)自定義下載工具操作異步線程類:
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; } @Override public void run() { try { URL httpUrl = new URL(downLoadUrl); HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setDoInput(true);// 如果打算使用 URL 連接進行輸入,則將 DoInput 標(biāo)志設(shè)置為 true;如果不打算使用,則設(shè)置為 false。默認(rèn)值為 true。 conn.setDoOutput(true);// 如果打算使用 URL 連接進行輸出,則將 DoOutput 標(biāo)志設(shè)置為 true;如果不打算使用,則設(shè)置為 false。默認(rèn)值為 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() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return super.shouldOverrideUrlLoading(view, url); } }); //要實現(xiàn)WebView文件下載,實現(xiàn)這個監(jiān)聽就ok test_wv.setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Log.v("ldm", url); if (url.endsWith(".apk")) {//判斷是否是.apk結(jié)尾的文件路徑 new DownLoadThread(url, MainActivity.this).start(); } } }); } }
方法2:通過系統(tǒng)自身下載方式下載(會在通知欄顯示下載進度條)
只需要把這個方法改寫如下:
test_wv.setDownloadListener(new DownloadListener() { @Override public 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); } });
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android文件操作技巧匯總》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android WebView實現(xiàn)文件下載功能
- Android使用WebView實現(xiàn)文件下載功能
- 解決Android SDK下載和更新失敗的方法詳解
- Android文件下載進度條的實現(xiàn)代碼
- Android Studio使用教程(一):下載與安裝及創(chuàng)建HelloWorld項目
- Android實現(xiàn)下載文件功能的方法
- 詳解Android使用OKHttp3實現(xiàn)下載(斷點續(xù)傳、顯示進度)
- android實現(xiàn)通知欄下載更新app示例
- Android zip文件下載和解壓實例
- Android在WebView中調(diào)用系統(tǒng)下載的方法
相關(guān)文章
Android判斷網(wǎng)絡(luò)類型的方法(2g,3g還是wifi)
這篇文章主要介紹了Android判斷網(wǎng)絡(luò)類型的方法,可實現(xiàn)判斷2g,3g還是wifi的功能,結(jié)合實例形式分析了Android針對網(wǎng)絡(luò)類型的相關(guān)判定技巧,需要的朋友可以參考下2016-02-02