自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

多線程下載Apk并提示安裝

移動開發(fā)
本項(xiàng)目是一個多線程下載應(yīng)用并提示是否安裝的小例子,從網(wǎng)上下載apk存儲到手機(jī)指定目錄,可以在通知欄顯示下載進(jìn)度進(jìn)度,下載完成后會有一個對話框提示用戶是否安裝,如果不需要可以刪除,項(xiàng)目有非常非常詳細(xì)的中文目錄,項(xiàng)目涉及知識:文件流、網(wǎng)絡(luò)下載鏈接協(xié)議、讀寫權(quán)限、Handler、Notification、跑馬燈。

源碼簡介
本項(xiàng)目是一個多線程下載應(yīng)用并提示是否安裝的小例子,從網(wǎng)上下載apk存儲到手機(jī)指定目錄,可以在通知欄顯示下載進(jìn)度進(jìn)度,下載完成后會有一個對話框提示用戶是否安裝,如果不需要可以刪除,項(xiàng)目有非常非常詳細(xì)的中文目錄,項(xiàng)目涉及知識:文件流、網(wǎng)絡(luò)下載鏈接協(xié)議、讀寫權(quán)限、Handler、Notification、跑馬燈。
源碼運(yùn)行截圖

源碼片段

 

  1. // 下載APK的線程匿名類START 
  2.     private Runnable mdownApkRunnable = new Runnable() { 
  3.         @Override 
  4.         public void run() { 
  5.             try { 
  6.                 URL url = new URL(apkDownloadPath); 
  7.                 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
  8.                 conn.connect(); 
  9.                 int length = conn.getContentLength(); 
  10.                 InputStream is = conn.getInputStream(); 
  11.   
  12.                 File file = new File(savePath); 
  13.                 Log.e("test", file.exists()+""); 
  14.                 if (!file.exists()) { 
  15.                     Log.e("test1", file.exists()+""); 
  16.                     file.mkdir(); 
  17.                     Log.e("test2", file.exists()+""); 
  18.                 } 
  19.                 String apkFile = saveFileName; 
  20.                 Log.e("test3", apkFile); 
  21.                 File ApkFile = new File(apkFile); 
  22.                 FileOutputStream fos = new FileOutputStream(ApkFile); 
  23.   
  24.                 int count = 0
  25.                 byte buf[] = new byte[1024]; 
  26.                 do { 
  27.                     int numread = is.read(buf); 
  28.                     count += numread; 
  29.                     progress = (int) (((float) count / length) * 100); 
  30.                     if(handmsg < progress){ 
  31.                         handmsg ++; 
  32.                         mHandler.sendEmptyMessage(DOWN_UPDATE); 
  33.                     } 
  34.                     // 更新進(jìn)度 
  35.                     if (numread <= 0) { 
  36.                         // 下載完成通知安裝 
  37.                         mHandler.sendEmptyMessage(DOWN_OVER); 
  38.                         break
  39.                     } 
  40.                     fos.write(buf, 0, numread); 
  41.                 } while (true);// 點(diǎn)擊取消就停止下載. 
  42.                 fos.close(); 
  43.                 is.close(); 
  44.             } catch (MalformedURLException e) { 
  45.                 e.printStackTrace(); 
  46.             } catch (IOException e) { 
  47.                 e.printStackTrace(); 
  48.                 Log.e("test", e.getMessage()); 
  49.             } 
  50.         } 
  51.     }; 

源碼鏈接:http://down.51cto.com/data/1968727

責(zé)任編輯:chenqingxiang 來源: 網(wǎng)絡(luò)整理
相關(guān)推薦

2015-02-03 15:06:23

android多線程下載

2011-05-31 13:29:40

Android 多線程

2012-12-25 11:39:20

Pythoncrawler

2009-03-12 10:52:43

Java線程多線程

2009-11-13 09:02:05

OpenSUSE系統(tǒng)下載

2015-12-15 13:16:13

CentOSLinux 7 151升級

2023-06-05 07:56:10

線程分配處理器

2023-06-06 08:17:52

多線程編程Thread類

2013-07-16 10:12:14

iOS多線程多線程概念多線程入門

2021-12-26 18:22:30

Java線程多線程

2010-01-21 11:27:30

linux多線程機(jī)制線程同步

2009-06-29 17:49:47

Java多線程

2010-08-19 10:15:57

Chrome 7

2020-12-28 08:03:26

多線程進(jìn)程瀏覽器

2011-07-21 11:12:58

iPhone 線程 多線程

2023-06-13 13:39:00

多線程異步編程

2021-06-11 11:28:22

多線程fork單線程

2011-06-02 17:27:49

iphone 多線程

2024-05-29 09:13:37

2023-06-07 13:49:00

多線程編程C#
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號