1. 程式人生 > >android從伺服器獲取apk安裝

android從伺服器獲取apk安裝

try {  
            URL url = new URL(params[0]);  
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
            connection.setConnectTimeout(10 * 1000); //超時時間 
            connection.connect();  //連線
            if (connection.getResponseCode() == 200) { //返回的響應碼200,是成功. 
                File file = new File("/mnt/sdcard/xxxx.apk");   
                file.createNewFile();  
                InputStream inputStream = connection.getInputStream();  
                ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); //快取 
                byte[] buffer = new byte[1024 * 10];  
                while (true) {  
                   int len = inputStream.read(buffer);  
                   publishProgress(len);  
                    if (len == -1) {  
                        break;  //讀取完
                    }  
                   arrayOutputStream.write(buffer, 0, len);  //寫入
                }  
                arrayOutputStream.close();  
                inputStream.close();  
 
                byte[] data = arrayOutputStream.toByteArray();  
                FileOutputStream fileOutputStream = new FileOutputStream(file);  
                fileOutputStream.write(data); //記得關閉輸入流 
                fileOutputStream.close();  
            }  
  
        } catch (MalformedURLException e) {  
.            e.printStackTrace();  
       } catch (IOException e) {  
            e.printStackTrace();  
        }  



 以上是讀取APK檔案並儲存在了本地,InputStream轉為FileOutputStream儲存HttpURLConnection獲取到的資料 。
 那麼只要再找到你的那個儲存的路徑就能實現安裝了。
  
   


下面是安裝和解除安裝的程式碼:  

首先說下解除安裝:

Uri packageURI = Uri.parse("package:xxx"); 

//package:xxx  這個形式是 package:程式完整的路徑 (包名+程式名).   

Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);   

startActivity(uninstallIntent);

注意:

Environment擁有一些可以獲取環境變數的方法 

然後是 安裝: 

String str = "/xxx.apk"; //APK的名字

String fileName = Environment.getExternalStorageDirectory() + str;   

//我們上面說到路徑

(1)

//不開啟APK程式程式碼

Intent intent = new Intent(Intent.ACTION_VIEW);  

intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");

startActivity(intent);

(2) 

//開啟APK程式程式碼
private void openFiles(File file) {     
                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction(android.content.Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file
),"application/vnd.android.package-archive");
                startActivity(intent);

        }  

 
當然拉 ,這裡不僅一種方法:以下方法也是可行的--
//下載apk程式程式碼
protected File downLoadFile(String httpUrl) {
                final String fileName = "dujinyang.apk";
                File tmpFile = new File("/sdcard/update");
                if (!tmpFile.exists()) {
                        tmpFile.mkdir();//建立資料夾
                }
                final File file = new File("/sdcard/update/" + fileName);


                try {
                        URL url = new URL(httpUrl);
                        try {
                                HttpURLConnection conn = (HttpURLConnection) url
                                                .openConnection();
                                InputStream is = conn.getInputStream();
                                FileOutputStream fileOutput= new FileOutputStream(file);
                                byte[] buf = new byte[256];//分配byte
                                conn.connect();
                                double count = 0;
                                if (conn.getResponseCode() >= 400) {
                                        Toast.makeText(Main.this, "連線超時", Toast.LENGTH_SHORT)
                                                        .show();
                                } else {
                                        while (count <= 100) {
                                                if (is != null) {
                                                        int numRead = is.read(buf);
                                                        if (numRead <= 0) {
                                                                break;
                                                        } else {
                                                                fileOutput.write(buf, 0, numRead);
                                                        }


                                                } else {
                                                        break;
                                                }


                                        }
                                }


                                conn.disconnect();//需要記得關閉連線
                                fileOutput.close();
                                is.close();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                } catch (MalformedURLException e) {                      e.printStackTrace();
                }


                return file;
        }