您当前位置:主页 > 程序开发 >
 
android 从服务器上获取APK并下载安装
日期:2019-01-17 编辑:AG集&#

[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;
        }

 

 

到这里 思路简单的理清 完了。

 

http://www.bkjia.com/Androidjc/538318.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/538318.htmlTechArticle[java] SPAN style=COLOR: #666666; FONT-SIZE: 24pxSTRONG简单的为新手做个分享。 /STRONG/SPAN SPAN style=COLOR: #cc0000 /SPANSPAN style=COLOR: #cc0000SPAN style=FONT-SIZE:...

本文源自: AG环亚娱乐

上一篇:qt超强绘图控件qwt 下一篇:没有了
 
Copyright © 2005-2016 http://www.tecsquared.com AG环亚娱乐_AG直营网_环亚娱乐AG最佳线路_腾讯体育版权所有 AG环亚娱乐_AG直营网_环亚娱乐AG最佳线路_腾讯体育