Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 從服務器上獲取APK並下載安裝

android 從服務器上獲取APK並下載安裝

編輯:關於Android編程

[java]
  
<SPAN style="COLOR: #666666; FONT-SIZE: 24px"><STRONG>簡單的為新手做個分享。 
</STRONG></SPAN>    <SPAN style="COLOR: #cc0000"> </SPAN><SPAN style="COLOR: #cc0000"><SPAN style="FONT-SIZE: 18px">網上有些資料,不過都是很零散,或是很亂的,有的人說看不懂。 
    一直有新手說 做到服務器更新APK時沒有思路,這裡做個簡單的分享,希望有不同思路的可以討論。 
    下面做個很簡單的讀取處理和講解思路。 
    代碼帶有注釋</SPAN>: 
</SPAN>  

 
簡單的為新手做個分享。
     網上有些資料,不過都是很零散,或是很亂的,有的人說看不懂。
    一直有新手說 做到服務器更新APK時沒有思路,這裡做個簡單的分享,希望有不同思路的可以討論。
    下面做個很簡單的讀取處理和講解思路。
    代碼帶有注釋:
  

[java
 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/yang/dujinyang.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();   
        }   

 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/yang/dujinyang.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獲取到的數據 。
 那麼只要再找到你的那個保存的路徑就能實現安裝了。
 
  

[java]
下面是安裝和卸載的代碼:   

下面是安裝和卸載的代碼:  [java] view plaincopyprint?
首先說下<STRONG><SPAN style="COLOR: #ff6666">卸載</SPAN></STRONG>:Uri packageURI = Uri.parse("package:com.demo.DUJINYANG");   

首先說下卸載:Uri packageURI = Uri.parse("package:com.demo.DUJINYANG");  [java] view plaincopyprint?
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);    

 Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);   [java] view plaincopyprint?
startActivity(uninstallIntent);Environment擁有一些可以獲取環境變量的方法  

startActivity(uninstallIntent);Environment擁有一些可以獲取環境變量的方法 [java] view plaincopyprint?
package:com.demo.DUJINYANG  這個形式是 package:程序完整的路徑 (包名+程序名).  

package:com.demo.DUJINYANG  這個形式是 package:程序完整的路徑 (包名+程序名). [java] view plaincopyprint?
  

 [java]
然後是 --<STRONG><SPAN style="COLOR: #ff0000">安裝</SPAN></STRONG>: String str = "/Dujinyang.apk"; //APK的名字 

然後是 --安裝: String str = "/Dujinyang.apk"; //APK的名字[java] view plaincopyprint?
String fileName = Environment.getExternalStorageDirectory() + str;    

String fileName = Environment.getExternalStorageDirectory() + str;   [java] view plaincopyprint?
//我們上面說到路徑Intent intent = new Intent(Intent.ACTION_VIEW);   

//我們上面說到路徑Intent intent = new Intent(Intent.ACTION_VIEW);  [java] view plaincopyprint?
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive"); 

intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");[java] view plaincopyprint?
startActivity(intent);  

 startActivity(intent); [java] 
  

 [java] 
<STRONG><SPAN style="COLOR: #ff0000">主要代碼如下</SPAN></STRONG>://打開APK程序代碼  
 
private void openFiles(File file) { 
                // TODO Auto-generated method stub  
                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程序代碼

private void openFiles(File file) {
                // TODO Auto-generated method stub
                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);
        }  [java]
  

 [java] 
當然拉 ,這裡不僅一種方法:以下方法也是可行的-- 

當然拉 ,這裡不僅一種方法:以下方法也是可行的--[java] 
//下載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; 
        } 

//下載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;
        }

 

 

到這裡 思路簡單的理清 完了。

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved