apk 下载

关于apk下载的一些问题
获取本地的版本号和服务器的版本号进行版本控制

PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
             final int version = info.versionCode;//版本号
             String versionname = info.versionName;//版本名称

连接服务器apk地址进行下载

ProgressDialog dialog =  ProgressDialog.show(MainActivity2.this, "提示", "正在下载中",  
                        false, false);
                downloadApk(dialog);
public void downloadApk(final ProgressDialog dialog ){
        new Thread() {
            public void run() {
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(pref.getString(Constant.Preference.SERVER)+Constant.Url.DOWN);
                HttpParams httpParams = httpClient.getParams();
                HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
                HttpConnectionParams.setSoTimeout(httpParams, 5000);
                HttpResponse httpResponse = null;
                try {
                    httpResponse = httpClient.execute(httpGet);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    if (httpEntity != null) {
                        InputStream is = httpEntity.getContent();
                        File dir = new File(
                                Environment.getExternalStorageDirectory()
                                        + MyConfig.DOWNLOADPATH);
                        if (!dir.exists()) {
                            dir.mkdir();
                        }
                        File file = new File(
                                Environment.getExternalStorageDirectory()
                                        + MyConfig.DOWNLOADPATH,
                                MyConfig.DOWNLOADAPK);
                        FileOutputStream fileOutputStream = new FileOutputStream(
                                file);
                        byte[] buff = new byte[1024];
                        int ch = -1;
                        while ((ch = is.read(buff)) != -1) {
                            fileOutputStream.write(buff, 0, ch);
                        }
                        fileOutputStream.flush();
                        fileOutputStream.close();
                        is.close();
                    }
                    handler.sendEmptyMessage(MyConfig.MSG_APKINSTALL);
                    dialog.dismiss();
                } catch (Exception e) {
                    // TODO Auto-generated catch block

                            Log.d("NetWorkUtils--->downApk---catch exception is "
                                    , e.getMessage());
                    e.printStackTrace();
//                  handler.sendEmptyMessage(MyConfig.MSG_DOWNLOADAPK_FAIL);
                }
            }
        }.start();

下载完成后安装apk

Intent intent = new Intent(Intent.ACTION_VIEW);
//MyConfig.DOWNLOADPATH  下载的apk存放文件将夹 "/jdjianxiu/" 
//MyConfig.DOWNLOADAPK  apk的名称
intent.setDataAndType(Uri.fromFile(new File(Environment
                        .getExternalStorageDirectory()+ MyConfig.DOWNLOADPATH ,
                        MyConfig.DOWNLOADAPK)),
                        "application/vnd.android.package-archive");
                startActivity(intent);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值