android 下載遠程服務器文件。


下載功能介紹,首先彈出一個進度對話窗,進度隨着文件的下載,進度條漸進變化。

1. 定義file文件。

2.主線程顯示進度框。  a)定義一個進度對話框,b)設置風格,c)顯示show

3.取得文件路徑,為下一步input 和output 做准備。

4.啟動子線程。詳細見注釋。

5.完成后,關閉。

6.啟動另一個intent,這個是安裝程序,是android的自帶的安裝打包程序。是從android的源碼里,packageinstaller里的manifest清單文件中取得的。固定寫法。

 

 

private File apkFile;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    public void downloadAPK(View v) {
        //1). 主線程, 顯示提示視圖: ProgressDialog
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.show();
        
        //准備用於保存APK文件的File對象 : /storage/sdcard/Android/package_name/files/xxx.apk
        apkFile = new File(getExternalFilesDir(null), "update.apk");
        
        //2). 啟動分線程, 請求下載APK文件, 下載過程中顯示下載進度
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                try {
                    //1. 得到連接對象
                    String path = "http://192.168.10.165:8080/Web_Server/L04_DataStorage.apk";
                    URL url = new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    //2. 設置
                    //connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(10000);
                    //3. 連接
                    connection.connect();
                    //4. 請求並得到響應碼200
                    int responseCode = connection.getResponseCode();
                    if(responseCode==200) {
                        //設置dialog的最大進度
                        dialog.setMax(connection.getContentLength());
                        
                        
                        //5. 得到包含APK文件數據的InputStream
                        InputStream is = connection.getInputStream();
                        //6. 創建指向apkFile的FileOutputStream
                        FileOutputStream fos = new FileOutputStream(apkFile);
                        //7. 邊讀邊寫
                        byte[] buffer = new byte[1024];
                        int len = -1;
                        while((len=is.read(buffer))!=-1) {
                            fos.write(buffer, 0, len);
                            //8. 顯示下載進度
                            dialog.incrementProgressBy(len);
                            
                            //休息一會(模擬網速慢)
                            //Thread.sleep(50);
                            SystemClock.sleep(50);
                        }
                        
                        fos.close();
                        is.close();
                    }
                    //9. 下載完成, 關閉, 進入3)
                    connection.disconnect();
                    
                    //3). 主線程, 移除dialog, 啟動安裝
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            dialog.dismiss();
                            installAPK();
                        }
                    });
                    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        //09-05 12:59:20.553: I/ActivityManager(1179): Displayed com.android.packageinstaller/.PackageInstallerActivity: +282ms
    }
    
    /**
     * 啟動安裝APK
     */
    private void installAPK() {
        Intent intent = new Intent("android.intent.action.INSTALL_PACKAGE");
        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        startActivity(intent);
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM