android 下載apk源碼

private void downloadFile(String urls) {
        String fileName="update.apk";
        File tmpFile=new File("/sdcard/update");
        if(!tmpFile.exists()){
        //創建文件目錄
            tmpFile.mkdir();
        }
        File file=new File("/sdcard/update/"+fileName);
        try {
            URL url=new URL(urls);
            HttpURLConnection conn=(HttpURLConnection)url.openConnection(); 
            InputStream in=conn.getInputStream();
            OutputStream ous=new FileOutputStream(file);

            byte[] bb=new byte[256];
            conn.connect();
            double count=0;
            if(conn.getResponseCode()>=400){
                Toast.makeText(this, "連接超時", 1).show();
            }else{
                while(count<=400){
                    if(in!=null){
                        int numRead=in.read(bb);
                        if(numRead<0){
                            break;
                        }else{
                            ous.write(bb, 0, numRead);
                        }
                    }else{
                        break;
                    }
                }
            }
            conn.disconnect();
            in.close();
            ous.close();


        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章