zoukankan      html  css  js  c++  java
  • apk 下载并自动安装

           今天带来的是apk的下载和自动安装,其实这个挺简单的,代码量不大,那就直接上代码吧! 
     

        第一步 是点击按钮进行:

        //按钮(Button)
        public void downAndInstall(View v) {
            // 下载apk,传入链接
            downLoadFile("http://static.nduoa.com/apk/569/569962/1287163/com.tencent.qqmusic.apk");
    
            // 自动安装
            AutoInstall.setUrl("/sdcard/update/updata.apk");
    
            AutoInstall.install(DownAndInstallActivity.this);
    
        }

    第二步就是下载apk

    // 下载apk程序代码
        protected File downLoadFile(String httpUrl) {
            final String fileName = "updata.apk";
            // 创建文件路径
            File temFile = new File("/sdcard/update");
            // 如果文件不存在。就创建
            if (!temFile.exists()) {
                temFile.mkdir();
            }
            // 得到文件路径
            final File file = new File("/sdcard/update/" + fileName);
    
            try {
                // 实例化URL对象,指定文件下载的地址
                URL url = new URL(httpUrl);
                // 获取HttpURLConnection 对象
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                // 得到字节读取流对象
                InputStream is = conn.getInputStream();
                // 实例化文件写入流,将下载的文件写入sd卡
                FileOutputStream fos = new FileOutputStream(file);
    
                byte[] buf = new byte[256];
    
                conn.connect();
    
                double count = 0;
    
                // 网络请求失败(getsponseCode==200时,请求成功)
                if (conn.getResponseCode() >= 400) {
                    Toast.makeText(DownAndInstallActivity.this, "连接超时",
                            Toast.LENGTH_SHORT).show();
                } else {
                    while (count <= 100) {
                        if (is != null) {
                            int numRead = is.read(buf);
                            if (numRead <= 0) {
                                break;
                            } else {
                                fos.write(buf, 0, numRead);
                            }
                        } else {
                            break;
                        }
                    }
    
                }
    
                conn.disconnect();
    
                // 关闭流
                fos.close();
                is.close();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return file;
        }

    第三步就是自动安装apk(这里是调用系统的安装,自然是辅助类)

    public class AutoInstall {
    
        private static String mUrl;
        private static Context mContext;
    
        /**
         * 外部传进来的url以便定位需要安装的apk
         * 
         * @param url
         */
    
        public static void setUrl(String url) {
            mUrl = url;
        }
    
        /**
         * 安装
         * 
         * @param context
         *            接收外部传进来的context
         * 
         */
    
        public static void install(Context context) {
            mContext = context;
            // 核心是下面几句代码
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(mUrl)),
                    "application/vnd.android.package-archive");
            mContext.startActivity(intent);
        }


          那么到这里apk的下载和自动安装就完成了,不过这里要说明的就是:这里的点击下载是在主线程实现的,在下载时一定会阻碍UI界面。具体怎么实现,这里就不过多讲解了

         如果网友们有什么好建议以及好的完善方法,请一定要不吝指教,让我多多学习啊!thanks

    2014-02-11

  • 相关阅读:
    在.net中使用redis(StackExchange.Redis)
    基于windows平台的命令行软件安装工具Chocolatey的安装
    c#的日志插件NLog基本使用
    微信小程序的组件总结
    markdown语法简介
    微信小程序基础语法总结
    leetcode-mid-sorting and searching
    leetcode-mid-sorting and searching
    leetcode-mid-sorting and searching -347. Top K Frequent Elements
    leetcode-mid-sorting and searching-34 Search for a Range
  • 原文地址:https://www.cnblogs.com/boyuanmeng/p/3544924.html
Copyright © 2011-2022 走看看